aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes JQuery: Accessing DOM elements in the same div as the clicked button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "JQuery: Accessing DOM elements in the same div as the clicked button" Watch "JQuery: Accessing DOM elements in the same div as the clicked button" New topic
Author

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

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

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: 61648
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

closest ! That's what I need. Thank you Bear.
 
 
subject: JQuery: Accessing DOM elements in the same div as the clicked button