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

Freezing Gems

What is a gem and why would you want to freeze it? In Ruby, there are times when you want to access pieces of functionality that other people of written (3rd party libraries) and you normally have 2 options. You can install a plug in or install a gem. Normally the method you use is determined by which ever is made available by the author. Gems are installed on the host machine and are pretty handy when you want to run things in the command line or else across lots of projects, but their downside is that if you use a gem in a Rails project there is no automatic publishing mechanism when you deploy your site. You will need to log onto the remote host machine and install the gem manually. Plugins are specific to Rails and are similar to gems in that they are also 3rd party libraries. However they are associated with your Rails project as opposed to your machine so they will get posted to the server on a regular deploy. Freezing a gem is the process of transforming a gem into a plug in. Es

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

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