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; |




























































One Comment
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.