How To Select Neighbor Element In Jquery (or Javascript)
I have a HTML code like this: 1 ).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.
Solution 2:
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.com/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>
Post a Comment for "How To Select Neighbor Element In Jquery (or Javascript)"