tags:

views:

149

answers:

2

Given a section of HTML, can I selectively pick some types of elements (ex: input type) and add a custom attribute using Javascript. I would also need to remove this attribute if it exists.

I have done this earlier using JQuery but am unable to use JQuery for this particular task.

+1  A: 
el.attribute = value

is all there is to it. The attribute is created if it does not exist.

Macha
He also asked about removal.
buti-oxa
+4  A: 

Accessing HTML attributes using the DOM

element.hasAttribute('foo');
element.getAttribute('foo');
element.setAttribute('foo', value);
element.removeAttribute('foo');
Luis Melgratti
addAttribute is not a method
KooiInc