Monday, June 30, 2008

Unobtrusive JavaScript and the "lang" attribute

One issue that has come up in using Unobtrusive JavaScript is how do you assign instance specific data to the element.

For example, in the old days (pre UJ) you could iterate over a bunch of objects (each with it's own ID) and hard code the ID into the on{Event} attribute

i.e.
<% for item in items %>
<div onclick="someFunction(<%= item.id %>)">Click here to select item</div>
<% end %>

But in UJ, on{Event} attributes are no longer considered good practice. If only there was a way we could somehow encode the id with the HTML like we could in Flex (Flex supports dynamic attributes on objects).

You can assign a dynamic attribute using JavaScript, but putting a script tag into the loop seems very inelegant and against UJ principles. The other way would be to render all the objects in JavaScript first and then re-render them in HTML. This does seem rather wasteful in terms of processing however and full of index dependencies when you want to merge the JS objects with their HTML siblings (extra loops, etc...).

My latest solution (which may not be 100% semantic XHTML compliant) is to use the oft ignored and hardly used "lang" attribute to store the ID data.

<% for item in items %>
<div lang="<%= item.id %>">Click here to select item</div>
<% end %>

I then pick it up in jQuery using

$('div').click(function(){
clickHandler(this);
});

function clickHandler(element) {
var id = $(element).attr("lang");
// do AJAX call with the id
}

The lang attribute is supposed to let the browser know what language to use to display the content, but when you think about it, isn't the content written in the language itself? Not only that, but this is mostly only really used for the HTML element and only in very specific cases would you need it on a div (case in point bilingual pages).

Whilst I will admit this is not a failsafe solution, it does seem to meet my needs. I am wondering if anyone else has come across this problem and what their solutions would be.

UPDATE
I just thought of a couple of alternative methods, but none that I like as much as the one mentioned in the article.

1) Loop through the elements in JS only and create the HTML elements dynamically using DOM creation.

Disadvantage: DOM creation is expensive. It also creates elements which are impossible to debug the HTML when viewing the source directly (though you can in Firebug).

2) Use a compound id tag and parse out the irrelevant data (i.e. id="article<%= item.id %>").

Disadvantage: The id attribute must start with a non numeric character so parsing is a requirement. You cannot have an id like id="365". Parsing is expensive and error prone.

1 comment:

Anthony B said...

I just thought of a couple of alternative methods, but none that I like as much as the one mentioned in the article.

1) Loop through the elements in JS only and create the HTML elements dynamically using DOM creation.

Disadvantage: DOM creation is expensive. It also creates elements which are impossible to debug the HTML when viewing the source directly (though you can in Firebug).

2) Use a compound id tag and parse out the irrelevant data (i.e. id="article<%= item.id %>").

Disadvantage: The id attribute must start with a non numeric character so parsing is a requirement. You cannot have an id like id="365". Parsing is expensive and error prone.