1

I have two different elements with the same id. How do I select the div element with id="abc" in jQuery.

For example:

<select id="abc">

  <option>jhon</option>

  <option>Richard</option>

</select>

<div id="abc">

</div>
1
  • 5
    NO! You CANNOT have the same id multiple times on the page. Commented Dec 18, 2011 at 15:29

3 Answers 3

3

It is given, that you cannot have multiple identic ID on same page.

The page won't be (X)HTML Valid DOM Document

If you do it, then you can't find elements by their id using javascript call getElementById(String)

See references:


For targeting multiple DIV elements on page, use .class instead

<select class="abc">
    <option>jhon</option>
    <option>Richard</option>
</select>
<div class="abc">

</div>

and find them by jQuery library

var divCollection = $(".abc");
Sign up to request clarification or add additional context in comments.

Comments

0

You CAN have multiple id's you should try to select it with the type of element you want $('select#abc') or $('div#abc'). You should never do it but its possible. I mean the word IDENTIFIER says it all or ;-) ?

2 Comments

You COULD have duplicate IDs, however you never, ever should. It is an extremely bad habit to get into and it shatters standards and rules. See @Marek Sebera's answer for references.
YEAH that's what I said, YOU SHOULD NEVER EVER DO IT !! But it is possible
0

When you try to just use javascript or jquery only the first id that is first select element will be taken and not the second one.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.