You are not Logged in
Would you like to Login or Register

Today is: Wednesday, 07 January, 2009
Check this months hot topics

Custom JavaScript attributes work differently in IE compared to Firefox

Adding custom attributes to HTML tags is a really valuable programming method. It's something I do quite often.

Consider the following code:

<script>
    function DoSomething(el){
        alert(el.contentId);
    }
</script>

<div contentId="123" editable="true" onclick="DoSomething(this)">Some Text</div>

The above code would work in IE6+ but would fail in Firefox.

The solution

In Firefox you need to use one of the following alternatives to read the attribute value back (these also work in IE)

 el.attributes["contentId"];

 el.getAttribute("contentId");

I guess we should all be using these rather than the helpful IE code... old habits die hard though.

kick it on DotNetKicks.com del.icio.us digg Mister Wong YahooMyWeb Reddit Furl Spurl blogmarks
Paul Hayman Skype
Author : Paul Hayman
Published : Wednesday, 01 August, 2007

Paul is the COO of kwiboo ltd consultant and has more than a decade of IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services. Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.

Comments

Philippe Leybaert said:

When you are using XHTML (as everyone should), you're not allowed to specify your own attributes on html tags. W3C XHTML validation will fail if you do.

The only way to add custom attributes (properties in fact) is by using JavaScript, like this:

document.getElementById("elementID").contentId = "123";

August 01, 2007 - 12:19 PM

Add Comment

Enter your comment below and it will be submitted for moderation.

Your Name

Add Tag

Please enter tags for this article, seperated by semi-colon ;

View Tag's by : # articles | # views