Duplicate IDs are baaaaaaaaaaaaaaad

If you are not already very aware, duplicate IDs in your markup are a very bad thing. Now, technically they will work fine in CSS, and can possibly even make it faster, BUT they WILL break your javascript. Basically when you have multiple elements with the same ID what the browser will do is unpredictable.

Sometimes in some browsers you might get what you want, other times in other browsers the wrong thing will happen, sometimes nothing will happen. You never know. On many different occasions I have been helping someone with some javascript, or fixing a website for a job and things just won’t work, and eventually I find the problem to be duplicate IDs. Dealing with this problem again today, I wrote a little jQuery plugin that checks your page for duplicate IDs. It is pretty simple, and you should never actually have this on a production site, it is just something you run while you are developing so you can yell at the jackass who wrote crappy markup to make sure you aren’t doing things you shouldn’t. The full code for the plugin is this:

(function($) {
	$.dupCheck = function() {
		var ids = [], dups = [], dupCount = 0;

		//this just displays the result if there is no console
		if (!window.console) {
			var console = {
				log: function(msg) {
					$('body').append(msg);
				}
			};
		} else {
			console = window.console;
		}		

		$('*').each(function() {
			var id = this.id, i = 0, len = ids.length, isDup = false;

			if(id) {
				for(i=0 ; i < len ; i++) { //loop over the IDs we have already processed to see if the current one is a dup
					if(!isDup && id === ids[i]) { //if the ID is a dup, add it to the dups list and set our bool
						if(checkIfAlreadyDup(id) === false) {
							dups.push(id);
						}
						isDup = true;
						dupCount++;
						break;
					}
				}

				if(!isDup) { //if the ID was not a dup, add it to the list of processed IDs
					ids.push(id);
				} else {	//if the ID was not a dup, clear out our bool
					isDup = false;
				}
			}
		});

		if(dups.length === 0) {
			console.log('No duplicate IDs found =)');
		} else {
			console.log('' + dups.length + ' duplicate IDs found repeated ' + dupCount + ' times =(');
		}

		function checkIfAlreadyDup(id) {
			var len = dups.length, i = 0;
			for(i = 0; i < len; i++) {
				if(dups[i] === id) {
					return true;
				}
			}
			return false;
		}		

		return dups;
	}
})(jQuery);

The way to use this is to load the plugin (or even just paste the code into your javascript console) then run the function:

	var dups = $.dupCheck();

You obviously need to do this after the document is ready, and after any offending javascript/ajax calls have completed.

It will output a message telling you how may duplicate IDs you have, and it returns an array of the IDs that are duplicates. It is pretty simple, but if you are banging you head against the wall trying to figure out why your javascript is not working, just run it, and it might give you the answer.

Download: Dupcheck.js

And I have now run this on this page, apparently I have a duplicate ID! Maybe I need to just get rid of that facebook ‘like’ button.

This entry was posted in javascript, jQuery, programming. Bookmark the permalink.

4 Responses to Duplicate IDs are baaaaaaaaaaaaaaad

  1. Pingback: 7 Ways to Make Your Site Friendly to DOM Manipulation | Monetate Engineering Blog

  2. Mark says:

    Whoa, things just got a whole lot esaier.

  3. Pingback: [RESOLVED]AJAX Control Inside A Usercontrol - Problem with duplicate ID's and javascript functionality | ASP Questions & Answers

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>