Skip to main content

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.

Comments

skuunk 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.

Popular posts from this blog

Master of my domain

Hi All, I just got myself a new domain ( http://www.skuunk.com ). The reason is that Blogspot.com is offering cheap domain via GoDaddy.com and I thought after having this nickname for nigh on 10 years it was time to buy the domain before someone else did (also I read somewhere that using blogspot.com in your domain is the equivalent of an aol.com or hotmail.com email address...shudder...). Of course I forgot that I would have to re-register my blog everywhere (which is taking ages) not to mention set up all my stats stuff again. *sigh*. It's a blogger's life... In any case, don't forget to bookmark the new address and to vote me up on Technorati !

Responsive Web Design

I wanted to go over Responsive Web Design using CSS. In the old days of web development, we had to code to common screen sizes (i.e. 800 X 600, 1024 X 768) and we patiently waited for people to upgrade their computers to have a decent amount of screen real estate so we could design things the way we really wanted. We also took on semi stretchy web layouts etc to expand and contract appropriately. Then about 2 or 3 years ago, Apple released this little device called an iPhone with a 320 X 480 resolution which took the world by storm and suddenly a lot of people were viewing your website on a tiny screen again... Anyways, as it can be difficult to design a site which looks good on 320 X 480 AND 1680 X 1050, we need to come up with some kind of solution. One way is to sniff the client and then use an appropriate stylesheet, but then you are mixing CSS with either JavaScript or server side programming and also potentially maintaining a list of appropriate clients and stylesheets. Also, you...

Elixir - destructuring, function overloading and pattern matching

Why am I covering 3 Elixir topics at once? Well, perhaps it is to show you how the three are used together. Individually, any of these 3 are interesting, but combined, they provide you with a means of essentially getting rid of conditionals and spaghetti logic. Consider the following function. def greet_beatle(person) do case person.first_name do "John" -> "Hello John." "Paul" -> "Good day Paul." "George" -> "Georgie boy, how you doing?" "Ringo" -> "What a drummer!" _-> "You are not a Beatle, #{person.first_name}" end end Yeah, it basically works, but there is a big old case statement in there. If you wanted to do something more as well depending on the person, you could easily end up with some spaghetti logic. Let's see how we can simplify this a little. def greet_beatle(%{first_name: first_name}) do case first_name d...