Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I cant get the element having same id as that of another id in another div . can any one help me ?

What I have tried:








if i want to name of id2. I tried like $('#id2 #name'). but i could not get.
Posted
Updated 19-Dec-16 10:16am

In HTML it is forbidden that more than one element has the same id. In other worlds id must be unique across the page. So if browsers bump into a non-unique id they will replace it with a random unique id. They do it mostly for the later element only, but some do it for all.
That means that when you search for an id you will get a maximum of one result.
It is not clear why would you give the same id for more then one element, but if you are looking for grouping use name attribute - that can be non-unique...
 
Share this answer
 
Comments
moyna coder 19-Dec-16 14:29pm    
So, what should i do ?.. only by attribute names ?
Kornfeld Eliyahu Peter 19-Dec-16 14:35pm    
I do no know what yor goal is or why, in the first olace, did you created the duplicate id, so I can not tell what to do...
moyna coder 19-Dec-16 14:39pm    
for some simplification i thought to use in that way and just wanted to know why was not possible.. thank you :)
ZurdoDev 19-Dec-16 16:13pm    
This is not true at all. Browsers will still work and display both controls and they will not change the id at all.
Kornfeld Eliyahu Peter 20-Dec-16 2:31am    
I didn't told browser will not work. Neither that browser will change your HTML... However the DOM will be different internally (every element will have a different id), and for that selecting by id will give you maximum of one results...
Quote:
if i want to name of id2. I tried like $('#id2 #name'). but i could not get.
You're close. You are missing a comma.

This would work:
JavaScript
$("#id2", "#name");  // find control with id=id2 inside of element with id = name


But Peter is right, you are much better off fixing whatever is causing duplicate IDs.
 
Share this answer
 
Comments
Kornfeld Eliyahu Peter 20-Dec-16 2:43am    
Taking this:
<div id="id1"><span id="name">1</span></div>
<div id="id2"><span id="name">2</span></div>

Your jQuery will return nothing, while getElementById will return the first... There is no way to reach the second span using id...
ZurdoDev 20-Dec-16 6:49am    
Sure it will. alert($("#name", "#id2").length) will be 1.
Kornfeld Eliyahu Peter 20-Dec-16 7:05am    
There is a big difference between $("#id2", "#name") and $("#name", "#id2")!!!
And this is one of the disturbing flaws of JQuery, that was created to handle multiply children elements of the same characteristic with different parent, but can be used to abuse HTML... The reason is that at this point jQuery actually traverses the DOM tree instead of using JavaScript functions...
ZurdoDev 20-Dec-16 7:13am    
Of course.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900