| Author |
JQuery: Accessing DOM elements in the same div as the clicked button
|
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
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 ?
|
[My Blog]
All roads lead to JavaRanch
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56224
|
|
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.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14672
|
|
|
closest ! That's what I need. Thank you Bear.
|
 |
 |
|
|
subject: JQuery: Accessing DOM elements in the same div as the clicked button
|
|
|