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 6090553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:13:38+00:00 2026-05-23T12:13:38+00:00

Possible Duplicate: JQuery to check for duplicate ids in a DOM Suppose i have

  • 0

Possible Duplicate:
JQuery to check for duplicate ids in a DOM

Suppose i have a code:

<div id="one">
    <div id="two"></div>
    <div id="three"></div>
</div>

<div id="four">
    <div id="two"></div>
</div>

<div id="one">
  <p id="five">
    <span id="three"></span>
  </p>
</div>

(a large HTML code with different DOM items).

Objective:

Is it possible to build a jQuery or JavaScript code that will alert me about duplication of ids within the document with the position. Here the position means like following;

> duplicate id: 'div#two' > within `div#four`, `div#one` 
> duplicate id: 'div#one' > parent of `p#five`
> duplicate id: 'span#three' > within `p#five` and such a pattern.

Note:

I found a problem similar to me, but not exact. As it is not duplicate of any question asked before. So don’t CLOSE IT.

  • 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-23T12:13:38+00:00Added an answer on May 23, 2026 at 12:13 pm

    NOTE: Read all caveats. The point of this code is to illustrate the nature of the problem, which is that a pure JS solution is inadvisable.

    First of all, hopefully what this is illustrates is that sometimes things that are doable are not always advisable. There are a ton of awesome tools out there that will provide far better error checking, like W3C’s validator or add-ins/extensions that utilize it, like Validity for Chrome. Definitely use those.

    But anyway, here’s a minimalist example. Note that none of the DOM has references to its own line number, so you have to get the entire innerHTML attribute from the documentElement as a string. You match parts of that string, then break it into a substring at the match position, then count the number of carriage returns. Obviously, this code could be extensively refactored, but I think the point is clear (also jsFiddle example for those who want it, although the lines will be fubar):

    EDIT

    I’ve updated the regex to not match examples like <div>id="a"</div>. Still, if the OP wants something pure JS, he’ll have to rely on this or a considerably more complex version with very minor benefits. The bottom line is that there are no associations between DOM nodes and line numbers. You will have to, on your own, figure out where the ID attributes are and then trace them back to their position. This is extremely error-prone. It might make some sense as programming practice but is extremely inadvisable in the real world. The best solution — which I’m reiterating for the fourth time here — is an extension or add-in that will just send your page on to a real validator like the W3C’s.

    The code below is designed to “just work,” because there is no good way to do what the OP is asking.

    <!DOCTYPE HTML>
    <html>
      <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>Test</title>
      </head>
      <body>
        <div id="a"></div>
        <div id="a"></div> <!-- catches this -->
        <div id="b"></div>
        <div>id="a"</div>
        <div id="c"></div>
        <div id="c"></div> <!-- catches this -->
        <span>[id="a"]</span>
        <script>
        var re = /<[^>]+id="(.*?)"[^>]*>/g; // match id="..."
        var nl = /\n\r?/g;                  // match newlines
        var text = document.documentElement.innerHTML;
        var match;
        var ids = {};                       // for holding IDs
        var index = 0;
        while (match = re.exec(text)) {
          // Get current position in innerHTML
          index = text.indexOf(match[0], index + 1);
    
          // Check for a match in the IDs array
          if (match[1] in ids) {
            // Log line number based on how many newlines are matched 
            // up to current position, assuming an offset of 3 -- one
            // for the doctype, one for <html>, and one for the current
            // line
            console.log("duplicate match at line " +
              (text.substring(0, index).match(nl).length + 3));
          } else {
            // Add to ID array if no match
            ids[match[1]] = null;
          }
        }
        </script>
      </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Detect element content changes with jQuery I have a div with a
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: jquery finish one animation then start the other one I've got a
Possible Duplicate: jquery/ajax load new content when available i have a table named news
Possible Duplicate: jquery trigger - ‘change’ function I have a radio button set called
Possible Duplicate: $(document).ready equivalent without jQuery I have a framework-less javascript that executes on
Possible Duplicate: How can I upload files asynchronously with JQuery? I have a file
Possible Duplicate: Check checkbox checked property using jQuery I am having an issue picking
Possible Duplicate: jQuery multiple class selector I have an input field '#q' where a
Possible Duplicate: NAnt or MSBuild, which one to choose and when? What is the

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.