user2227242
user2227242

Reputation: 11

How to add a new attribute to a tag

<input  id="test" value="123" name ="test"  verify="NOTNULL">

In some HTML, I saw the input tag has the attribute verify. How to add it to the input tag?

I tried the code

alert(document.getElementById("test").verify)  

the result is undefined.

How do I add a new attribute to the element?

Upvotes: 1

Views: 2018

Answers (2)

Icarus
Icarus

Reputation: 63966

Adding an attribute with pure Javascript is done via setAttribute

document.getElementById("test").setAttribute("verify","verified");

And to read it:

document.getElementById("test").getAttribute("verify")

Upvotes: 1

zzzzBov
zzzzBov

Reputation: 179046

If you mean to say that the html input element has a [verify] attribute, then you can access the attribute via getAttribute():

HTML:
<input id="test" verify="test" />
JS:
document.getElementById('test').getAttribute('verify'); //test

Upvotes: 5

Related Questions