File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Need help selecting a value from a list with multiple ids that are the same 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 "Need help selecting a value from a list with multiple ids that are the same" Watch "Need help selecting a value from a list with multiple ids that are the same" New topic
Author

Need help selecting a value from a list with multiple ids that are the same

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Hi

I have a list in this basic format (2 items in the following example - be aware I had to put onClick outside of the <'li'> element, otherwise it was not displaying correctly):

As you can see onClick I call address_dropdown_select passing in the address_id, country and html of the item. I need a way to get the value of customer_name depending on which item is clicked on. I can use javascript or jQuery to accomplish this, but will listen to any other suggestions.
I can probably add in a 4th param that I pass which would be the customer_name value, but I would like to avoid this if I can. Can anyone give me some guidance on this?


True wisdom is in knowing you know nothing - Socrates
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61190
    
  66

It is not valid to have multiple elements with the same id and nothing will work as expected until you fix this.


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

Joined: Nov 08, 2001
Posts: 15376
    
    6
See the html spec for ids: http://www.w3.org/TR/html4/struct/global.html#h-7.5.2

HTML5 has same restriction, but has different naming requirements.

Eric
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Bear Bibeault wrote:It is not valid to have multiple elements with the same id and nothing will work as expected until you fix this.


Hmm well I must have mis stated something or be missing something, because this has been working like this for quite some time now. I am not sure what you mean when you say nothing will work as expected.
I will see what else I can figure out :/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61190
    
  66

If you have invalid HTML there is no telling what will happen -- it will be non-deterministic.

Fix the page such that each id is unique -- anything else is irresponsible.
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Well unfortunately I do not have direct access to fix this myself, but I have passed it along. Thanks for the help. Unfortunately for now I still have to work with this as it is, so I am going to have to figure something else out.

So I know that the thing to do is fix this id issue, but putting that aside for now - could there be a way to work with this as it is and accomplish what I am trying to do?
I notice that I can use $(this).html(); to return the html of one list item at a time. Is it possible to extract the value I need from that by id? Something like $(this).html().($("#customer_name"))
I know that probably will not work, but just to give an idea of what I mean. I know I can probably get it using something like substr and/or instr, but that is not exactly desirable.
Someone also mentioned XPath as a possibility.

Also, the display of this list is incorrect in IE9 (but not IE8, FF or chrome) - the list is off to the right, but corrects itself once you select one of the items - could the id issue mentioned cause this kind of thing?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Well it has the class set, so use that, also why are you using onclick in the html, add it dynamically!



Eric


Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

Eric Pascarello wrote:Well it has the class set, so use that, also why are you using onclick in the html, add it dynamically!



Eric




Ah that is one of the things I would change if I had control over it.
But your suggestion (slightly tweaked) works well:

Basically the only difference is I am using the div id rather than the class as the selector.

Perhaps you can help me understand why something similar I tried would not work:

Either way, thanks to all for the help and suggestions.



EDIT


I didn't realize it earlier, but I also needed to use the following to convert this to useable String
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help selecting a value from a list with multiple ids that are the same