Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jQuery: Trying to get id of next element with click

 
Toni Howlett
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic