This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JQuery: Accessing DOM elements in the same div as the clicked button

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd like to what is the recommended way to achieve the following using JQuery. This is a very common situation, where I'm listing records, which have update/delete buttons. Something like this :

This div is inside a loop.
I set the onclick event of all delete buttons like this :


What's the recommended way to get the value of the record name (input type="text") which is inside the same div as the button which was clicked ? Do I have to use an index in the loop and give unique ids to all my inputs ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64629
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I often use .closest() to find a common parent of the event target.

To find the div:(where this is the event target within the handler)

You can then use parent as the context of subsequent selectors.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
closest ! That's what I need. Thank you Bear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic