3.7.06

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
="text/javascript"
>
var tag = document.getElementById('test');
alert(tag.custom1);
// undefined for moz
alert(tag.getAttribute('custom1')); // true

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

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

</script>

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

No comments: