This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply 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: 14672
    
  11

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
    
  13

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
    
  11

closest ! That's what I need. Thank you Bear.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: JQuery: Accessing DOM elements in the same div as the clicked button
 
Similar Threads
Need help with Jquery
Can't get RequestDispatcher forward() method or response.sendRedirect() to work
Ajaxstart/Ajaxstop Global Handler Form Reference
jQuery AJAX display issue
jQuery not working on page