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