Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Village Idiot
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not valid to have multiple elements with the same id and nothing will work as expected until you fix this.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 484
jQuery Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic