JavaScript Selecting only one element with jQuery

I've got a table with a number of items. I'm trying to show input fields when a user clicks on an edit button, but I don't know how to select only the element within the same row as the edit button. Here's the table structure:

<table>

    <tr class="table-row">
        <td>
          <a target='_blank' href="#" class="item-name">Item 1</a>
          <input type="text" value="[item]" class="input-item" id="item-1">
        </td>
        <td>
          <a target='_blank' href="#" class="item-name">Price 1</a>
          <input type="text" value="[item]" class="input-item" id="price-1">
        </td>
        <td><a target='_blank' href="#" class="edit-button">Edit</a></td>
    </tr>

    <tr class="table-row">
        <td>
          <a target='_blank' href="#" class="item-name">Item 2</a>
          <input type="text" value="[item]" class="input-item" id="item-2">
        </td>
        <td>
          <a target='_blank' href="#" class="item-name">Price 2</a>
          <input type="text" value="[item]" class="input-item" id="price-2">
        </td>
        <td><a target='_blank' href="#" class="edit-button">Edit</a></td>
    </tr>

    ...

</table>

What I intend to happen, is that when the user clicks on the Edit link within a specific row, the input fields will appear (in the 1st and 2nd columns) and the text will disappear, allowing the user to edit from the table itself. Problem is, the jQuery code I have (obviously) selects all the .input-item classes, thus showing all the inputs from all the rows.

$('.edit-button').click(function(event){
  $('.input-item').show();
});

How do I use jQuery to select only the inputs within the .table-row of the clicked Edit link?

Answer:1

You need traverse up to .table-row using .closest() method in current element context i.e. this, then use .find() to target descendant .input-item elements.

$('.edit-button').click(function(event){
  $(this).closest('.table-row').find('.input-item').show();
});
Answer:2

You can try this:

  $(this).closest('tr.table-row').find('.input-item').show();
Answer:3

Hey guys i am trying to create empty array in react-native using es6. But while accessing it i get an error data not defined. Here is the code snippet which i am using to initialise the array. ...

Hey guys i am trying to create empty array in react-native using es6. But while accessing it i get an error data not defined. Here is the code snippet which i am using to initialise the array. ...

  1. initialise array react
  2. initialize array react
  3. initialize array react native

I have an array eg: a[3,4,5,6,7,8]. I want to remove all the element in one time and make array as an empty array. How to remove all the element of an array. My code var a = [2,3,4,5,6]; for(var i=...

I have an array eg: a[3,4,5,6,7,8]. I want to remove all the element in one time and make array as an empty array. How to remove all the element of an array. My code var a = [2,3,4,5,6]; for(var i=...

  1. remove elements from list python
  2. remove elements from array
  3. remove elements from a list
  4. remove elements from array java
  5. remove elements from array matlab
  6. remove elements from array python
  7. remove elements from vector c++
  8. remove elements from numpy array
  9. remove elements from dictionary python
  10. remove elements from vector r
  11. remove elements from list java
  12. remove elements from list r
  13. remove elements from array ruby
  14. remove elements from string python
  15. remove elements from set python
  16. remove elements from string
  17. remove elements from array c#
  18. remove elements from dom
  19. remove elements from list python by index
  20. remove elements from np array

I am trying to create a sidebar with CSS while also having a topbar in the same window. I have a <div id="topbar"> and a <div id="sidebar_left"> in my html file. In the topbar there are ...

I am trying to create a sidebar with CSS while also having a topbar in the same window. I have a <div id="topbar"> and a <div id="sidebar_left"> in my html file. In the topbar there are ...

  1. creating sidebar with bootstrap
  2. create sidebar with elementor
  3. create sidebar with bootstrap 4
  4. create sidebar with flexbox
  5. create sidebar with css
  6. create sidebar with js
  7. create sidebar with header
  8. creating sidebar in html
  9. creating sidebar in android studio
  10. creating sidebar in wordpress
  11. creating sidebar in angular
  12. creating sidebar in squarespace
  13. create sidebar in react
  14. create sidebar in word
  15. create sidebar in reactjs
  16. create sidebar in laravel
  17. create sidebar in react native
  18. create sidebar in angular 6
  19. create sidebar in shopify
  20. create sidebar in angular 7

I'm trying since 4 hours to arrange 4 div side by side vertically.Those div are also having 2 div inside it,for image and paragraph.I'm able to arrange the 4 image div next to each other but paragraph ...

I'm trying since 4 hours to arrange 4 div side by side vertically.Those div are also having 2 div inside it,for image and paragraph.I'm able to arrange the 4 image div next to each other but paragraph ...