Skip to content Skip to sidebar Skip to footer

Javascript DOM Identifying Nodes By User Type?

If I have a tbody with some tr children nodes, how can I give each tr a user specified value that can be used to identify it? For example I want to do something like: mytr1 = mytbo

Solution 1:

You could use datasets.
It's HTML5, but it's fairly supported. Datasets support

Here's how to use them :

mytr1 = mytbody.createElement('tr');
mytr2 = mytbody.createElement('tr');

mytr1.dataset.userident = 'firstname';
mytr2.dataset.userident = 'lastname';

tbody.appendChild(mytr1);
tbody.appendChild(mytr2);


For accessing them,
implying you're using event binding and handling :

var userident = evt.target.dataset.userident;


EDIT

Actually it's supported by all browsers right now.
The only partially supported part is the access to the .dataset property.
Simply use .getAttribute('data-userident') and .setAttribute('data-userident','firstname') on your elements.


Post a Comment for "Javascript DOM Identifying Nodes By User Type?"