Skip to content Skip to sidebar Skip to footer

How To Select Neighbor Element In Jquery (or Javascript)

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)"