tags:

views:

2202

answers:

5

I'm writing applications with ASP.NET MVC. In contrast to traditional ASP.NET you're a lot more responsible for creating all the ids in your generated page. ASP.NET would give you nasty, but unique ids.

I'd like to add a quick little jQuery script to check my document for duplicate ids. They may be ids for DIVS, images, checkboxes, buttons etc.

<div id="pnlMain"> My main panel </div>
<div id="pnlMain"> Oops we accidentally used the same ID </div>

I'm looking for a set and forget type utility that'll just warn me when I do something careless.

Yes i'd be using this only during testing, and alternatives (such as firebug plugins) are welcome too.

+6  A: 

Why don't you just validate your html?

Double ID's are not allowed, and normally you will get a parse-error.

Natrium
what options are there for this?
Simon_Weaver
Also in FF, use the web developer toolbar under tools it has validators
NTulip
+8  A: 

You should try HTML Validator (Firefox extension). It'll definitely tell you the page has duplicate ids and much more.

Ionuț G. Stan
+1  A: 

This might do the trick It will alert all the ids of elements with duplicates.

<html>
    <head>
     <script type="text/javascript" src="jquery-1.3.1.min.js"></script>
     <script type="text/javascript">
      function findDupes()
      {
        var all = $("*");
        for(var i = 0; i < all.length; i++)
        {
            if (all[i].id.length > 0 && $("[id='" + all[i].id + "']").length > 1) alert(all[i].id);
        }
      }
     </script>
    </head>
    <body onload="findDupes()">
     <div id="s"></div>
     <div id="f"></div>
     <div id="g"></div>
     <div id="h"></div>
     <div id="d"></div>
     <div id="j"></div>
     <div id="k"></div>
     <div id="l"></div>
     <div id="d"></div>
     <div id="e"></div>
    </body>
</html>
Stephen lacy
+7  A: 

The following will log a warning to firebug's console.

// Warning Duplicate IDs
$('[id]').each(function(){
  var ids = $('[id='+this.id+']');
  if(ids.length>1 && ids[0]==this)
    console.warn('Multiple IDs #'+this.id);
});
sunsean
perfect! thanks! already discovered three places where I have duplicate IDs. it frustrates me slightly that most peoples solution to this problem is to use 'firebug' or 'html validator'. thats not good enough! i want to catch the unexpected duplicates in wierd situations.
Simon_Weaver
hehe and i switched console.warn to alert(...) so i HAVE to fix them :)
Simon_Weaver
have found this extremely useful and valuable. i think it ought to be a standard in frameworks - especially during debugging
Simon_Weaver
Awesome tip! Thanks!
Raj
+2  A: 

Yet another way of locating duplicates but this will add a class of error so it will have red text:

// waits for document load then highlights any duplicate element id's
$(function(){ highlight_duplicates();});

function highlight_duplicates() {
  // add errors when duplicate element id's exist
  $('[id]').each(function(){ // iterate all id's on the page
    var elements_with_specified_id = $('[id='+this.id+']');
    if(elements_with_specified_id.length>1){
      elements_with_specified_id.addClass('error');
    }
  });


  // update flash area when warning or errors are present
  var number_of_errors = $('.error').length;
  if(number_of_errors > 0)
    $('#notice').append('<p class="error">The '+number_of_errors+
      ' items below in Red have identical ids.  Please remove one of the items from its associated report!</p>');
}
Joshaven Potter
thats kinda cool! thanks. i've actually found the original accepted answer invaluable. caught so many things and saved probably hours of time!
Simon_Weaver
Cool, but why not just use console functions and let them do the rest? Separation of logic and presentation etc etc...
Will Morgan