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 );
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><table><tbody><trid="15"><td>1</td><tdclass="hidden-mob"><ahref="page.php"><iclass="fa fa-caret-up">x</i></a></td><tr><trid="16"><td>1</td><tdclass="hidden-mob"><ahref="page.php"><iclass="fa fa-caret-up">x</i></a></td><tr></tbody></table>
Post a Comment for "How To Select Neighbor Element In Jquery (or Javascript)"