Cross browser tag attributes

Granted, it's not simple to do it right to start with. In order to do it right (validate and blah blah), you'll need custom DTDs . Let's assume we don't care for now >:)

Long story short: IE will be friendly enough to expose the tag attributes as properties of the object. Code speaks louder than english, so:

<input name="test" id="test" custom1="true" />
<script type
var tag = document.getElementById('test');
// undefined for moz
alert(tag.getAttribute('custom1')); // true

tag.setAttribute('custom2', 'true');
//undefined for moz
alert(tag.getAttribute('custom2')); // true

tag.custom3 = true;
// true
alert(tag.getAttribute('custom3')); // null for moz


Moral: Stick to getAttribute() for attributes. Use the property for properties. Simple huh?

No comments: