Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6471037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:10:45+00:00 2026-05-25T06:10:45+00:00

Is it safe to give several elements the same ID in one page? For

  • 0

Is it safe to give several elements the same ID in one page? For example this often happens, when using some jquery plugins, when you run some slider or gallery twice or more. We know, developers like to give some ID to the html container in order the script works faster.

Let’s read w3.org documentation:

What makes attributes of type ID special is that no two such
attributes can have the same value; whatever the document language, an
ID attribute can be used to uniquely identify its element.

But the next example with 2 elements having the same ID works fine in all browsers, though it’s not valid:

#red {
  color: red;
}
<p id="red">I am a red text.</p>
<p id="red">I am a red text too.</p>

Can anybody explain this strange situation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:10:46+00:00Added an answer on May 25, 2026 at 6:10 am

    Browsers always try to “fail silently”. What this means is that even though your HTML is invalid, the browser will try to guess what your intent was, and handle it accordingly.

    However, deviating from the spec can cause some very unforeseen side effects.

    For example:

    document.getElementById('red');
    

    will only get you the first one.

    You’ll also have to test your page in all the browsers that your users might use, to make sure your code works as intended. You can’t just assume that it’ll work.

    In short: Don’t do this! If you need to target several elements with the same CSS, use a class name. That’s what they were designed for…


    Having said that; if you really need to select multiple elements with the same ID, use an attribute selector:

    document.querySelectorAll('p[id="red"]');
    

    Note however, that this doesn’t work in IE7 and below…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody give me example that NSMutableArray is thread safe or not?
First - what happens if I don't give a passphrase? Is some sort of
Is this 100% safe against XSS? If not, can you please provide example bad
I am wondering if anyone can give some safe guidelines on the maximum level
I just made a Java n-tuple which is type-safe. I'm using some unconventional methods
We're currently using Visual Source Safe and BugNet and looking to migrate up and
This question more falls into the category of best practices, and clean/safe code for
Is this a safe way to trim the newline character off of a line
While answering this question regarding safe escaping of filename with spaces (and potentially other
This is about thread safety of std::map . Now, simultaneous reads are thread-safe but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.