• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JQuery Selector Question Regarding Element Reuse

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm working on an application that uses tabs to display the forms. One of the requirements is that most of the edit forms open in their own tabs. For example, if I need to edit 4 categories I could open the edit form in 4 tabs, one for each category. All the markup between the tabs is the same.

To keep jquery from selecting the wrong elements on the wrong tabs I've created a parent div on each tab with a unique id. Something like:

Where 1234 is the category id. Then to select elements I just do:


1234 is actually not hard coded. I use what I get from the server. Is this a good approach? Is there a better way of achieving the same thing?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If all of the tabs are part of the same DOM, then the elements need to have unique ids. If more than one element has the id some_button, for example, things will go awry.

The parent id idea is a good one -- I use it often myself -- but you might want to think about using class names rather than ids to locate the tab children in order to avoid conflicting element ids.
[ September 09, 2008: Message edited by: Bear Bibeault ]
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bear Bibeault:
If all of the tabs are part of the same DOM, then the elements need to have unique IDs. If more than one element has the id some_button, for example, things will go awry.



Right, but if I always select some_button by a parent unique element, am I ok or is it more basic than that in that every DOM element must have a unique ID?
[ September 09, 2008: Message edited by: Gregg Bolinger ]
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot have two or more elements with the same id on a page at the same time or you run into problems. You can remove an element with an id and reuse that id.

Eric
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yea, this just turned into a bigger mess than what I was hoping for.
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Messes are fun to clean up with Ajax. Ajax is a great cleaner that gets into those nooks and crannies like nothing else.

Might be easier to dum the name and use the CSS selector markup if the divs are always going to be in the same order.

Eric
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Eric Pascarello:
Might be easier to dum the name and use the CSS selector markup if the divs are always going to be in the same order.

Eric



That's not a bad idea. Everything should be in the same order. It's all the same JSP, just different categories.
 
reply
    Bookmark Topic Watch Topic
  • New Topic