wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes jQuery: Trying to get id of next element with click Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "jQuery: Trying to get id of next element with click" Watch "jQuery: Trying to get id of next element with click" New topic
Author

jQuery: Trying to get id of next element with click

Toni Howlett
Ranch Hand

Joined: Jan 18, 2012
Posts: 35

This is the shortened version of my code, (no need for all of it), and my goal is that when I click "Edit" that it will show the div with the id XXX. Though to do this, I need to get the id XXX.... how?

I will have many of these on one page and I only want one function





Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60763
    
  65

It doesn't appear that there is any relationship whatsoever between the Edit "button" and the element you want to address. is that something that got lost in translation? Or is there really no relationship whatsoever?

If not, then you need to do something that relates them. One way that I've used is to tack a custom attribute onto the event target to know what related element is to be addressed.

Something like:

That way, you can obtain the value of the custom attribute in the handler.

P.S. If you are using jQuery 1.7, live() is deprecated.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Well it is possible to do it with closest and next, but it is very fragile. If someone adds a new element or moves the placement of things it will break. Basic idea:



See it in action: jsfiddle

Also using a span is not keyboard friendly [unless some library changes it to be.]

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60763
    
  65

Eric Pascarello wrote:Well it is possible to do it with closest and next, but it is very fragile.

Right. I will sometimes use structural relationships if its something that's unlikely to exhibit fragility. But usually a direct method by id is pretty bullet-porrf.
Toni Howlett
Ranch Hand

Joined: Jan 18, 2012
Posts: 35
Thanks Eric, that did exactly what I wanted!

And dont worry, my span tag has other classes on it, it makes it more like a button.


also Bear, I am using jQuery 1.6.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jQuery: Trying to get id of next element with click
 
Similar Threads
In Need of Urgent help Struts + jquery
Display result is reverted back (JSP and JQuery)
Expand and Collapse button: how to find the status of the button
Need Help with Jquery Selector logic
How to update a image using servlet?