admin管理员组文章数量:1389750
I have a HTML code like this:
<tr id="15">
<td id="NV">1</td>
<td class="hidden-mob">
<a href="page.php"><i id="c-voteup" class="fa fa-caret-up"></i></a>
</td>
<tr>
<tr id="16">
<td id="NV">1</td>
<td class="hidden-mob">
<a href="page.php"><i id="c-voteup" class="fa fa-caret-up"></i></a>
</td>
<tr>
Now I want to increase the value of the first <td>
in which <tr>
I click on its <i>
(neighbor <td>
).
For example: If I click of <i>
where <tr id="15">
then I want to increase the value of its <td>
like this: <td id="NV">2</td>
. Is it possible ?
Here is my try: I can not implement it fully, just I know about it scatteredly ...
$("i").click(function(e) {} // when click on <i>
var value = $("#NV").text().replace(/[^0-9]/g, ''); // getting current value
value++; // increase its value
$("#NV").html(value); // writing new value
Just my problem is selecting, How to select $("#NV")
that I've clicked on <i>
in the same <tr>
?
I have a HTML code like this:
<tr id="15">
<td id="NV">1</td>
<td class="hidden-mob">
<a href="page.php"><i id="c-voteup" class="fa fa-caret-up"></i></a>
</td>
<tr>
<tr id="16">
<td id="NV">1</td>
<td class="hidden-mob">
<a href="page.php"><i id="c-voteup" class="fa fa-caret-up"></i></a>
</td>
<tr>
Now I want to increase the value of the first <td>
in which <tr>
I click on its <i>
(neighbor <td>
).
For example: If I click of <i>
where <tr id="15">
then I want to increase the value of its <td>
like this: <td id="NV">2</td>
. Is it possible ?
Here is my try: I can not implement it fully, just I know about it scatteredly ...
$("i").click(function(e) {} // when click on <i>
var value = $("#NV").text().replace(/[^0-9]/g, ''); // getting current value
value++; // increase its value
$("#NV").html(value); // writing new value
Just my problem is selecting, How to select $("#NV")
that I've clicked on <i>
in the same <tr>
?
- .sibling() in jQuery. – Chrillewoodz Commented Sep 11, 2015 at 14:41
- @Chrillewoodz thanks, I read about it, I think it can be useful for my question. – Shafizadeh Commented Sep 11, 2015 at 14:45
2 Answers
Reset to default 3Try this:
$("i").click(function (e) { // when click on <i>
var $nv = $(this).closest('tr').find(".NV");
var value = $nv.text().replace(/[^0-9]/g, ''); // getting current value
value++; // increase its value
$nv.html(value);
});
- Please change all
#NV
ids to class as IDs are supposed to be unique in DOM.
You can use $(this).closest('td').prev()
to locate the td
of interest. However, you must not use duplicate IDs.
$('a > i').on('click', function(e) {
//prevent default action
e.preventDefault();
//locate the TD with value
var valTD = $(this).closest('td').prev();
//increment it's value
valTD.text( +valTD.text() + 1 );
});
$('a > i').on('click', function(e) {
//prevent default action
e.preventDefault();
//locate the TD with value
var valTD = $(this).closest('td').prev();
//increment it's value
valTD.text( +valTD.text() + 1 );
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table><tbody>
<tr id="15">
<td>1</td>
<td class="hidden-mob">
<a href="page.php"><i class="fa fa-caret-up">x</i></a>
</td>
<tr>
<tr id="16">
<td>1</td>
<td class="hidden-mob">
<a href="page.php"><i class="fa fa-caret-up">x</i></a>
</td>
<tr>
</tbody>
</table>
本文标签: How to select neighbor element in jquery (or javascript)Stack Overflow
版权声明:本文标题:How to select neighbor element in jquery (or javascript) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744660883a2618239.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论