The pitfalls of calling things “id”
Summary: Don’t call your form inputs “id”, they will clash with the Sizzle engine used in jQuery.
Hi! I’m Victor, one of Coupa’s newest Rails developers. I started working here fresh out of college in June 2010, but I finally persuaded them to give me a blog account. Today’s story is about why naming a form input “id” will break several selectors used by jQuery in IE, or cause forms to have weird ID attributes in other browsers.
I was merrily using all the new Unobtrusive Javascript (which is, ironically, a rather obtrusive name) features offered by Rails 3, when I hit a roadblock – they broke in IE. It was throwing a cryptic ‘Object does not support this method or property’ error every time I hit a particular page. Grumbling, I got the torches ready for another ritual sacrifice to the Internet Explorer gods.
For the first time ever, IE8 includes a handy in-browser Javascript debugger. It’s actually not half bad. Using it, I discovered the error was thrown here:
jquery-1.4.3.js
// qSA works strangely on Element-rooted queries
// We can work around this by specifying an extra ID on the root
// and working up from there (Thanks to Andrew Dupont for the technique)
// IE 8 doesn't work on object elements
} else if ( context.nodeType === 1 && context.nodeName.toLowerCase() !== "object" ) {
var old = context.id, id = context.id = "__sizzle__";
try {
return makeArray( context.querySelectorAll( "#" + id + " " + query ), extra );
Check out line 3904. See anything shady? Keep in mind that context is a DOM element.
Turns out, the DOM object namespace is pretty cluttered. You can normally access an attribute foo on an element with elem.foo. However, should that element be a form with an input named foo, this will actually return the input element. As a result, context.id on a form element won’t always give you the ID attribute. IE barfs immediately on that line since it’s trying to assign a value to a tag. Webkit and Gecko, on the other hand, will run happily, stringifying the input to [object HTMLInputElement], which is decidedly unhelpful as an ID attribute.
The page that was breaking in IE indeed had a form with an input named id. The interim solution, of course, is to rename your input tags. If you’re writing a library, use the getAttribute() DOM function instead of the dot notation shorthand to make sure you’re getting the attribute.
Moral of the story is, watch out when you’re naming things! Avoid possibly-important words like id, name, or class. And realize that once in a blue moon, IE will actually be helpful in debugging. I don’t think I could’ve figured out why my ID attributes were getting clobbered in FF and Chrome, had IE not crashed so opportunely.











Company Blog
Developer Blog
News & Events