Attaching information to a dom node using javascript

I had a requirement in my project where i had to attach some information to a dom node using javascript.
I created some checkboxes using DOM’s createElement method. Now the task was to assign userId information to each checkbox.
I knew that i can’t use the userId attribute for the “input” tag.

1
<input id="chk1" type="checkbox" />

The above code does not work.

So i did the following to attach userId info to the checkbox.

1
2
3
4
var chkBox=document.createElement("input");
chkBox.type="checkbox";
chkBox.id="chk1";
chkBox.userId="devang@cisco.com";

And this is how i retrieved the userId info attached to the checkBox.

1
var userId=document.getElementById('chk1').userId;
This entry was posted in WEB 2.0, javascript and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. John
    Posted February 26, 2009 at 4:23 pm | Permalink

    You could also put the value you want in the value attribute…

    .setAttribute(’value’,'whatever you want’);

    Though your technique does come in handy when you want to store “other”, “extra” data.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*