12

Any ideas how I would go about writing a javascript method to insert an attribute to a tag

eg. I have

<input id='in1' value='Submit' type='submit'/>

and I want to insert an attribute name

<input id='in1' name='submit_content' value='Submit' type='submit'/>

Thanks

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
mark
  • 125
  • 1
  • 1
  • 6

2 Answers2

28

Try this:

document.getElementById("in1").setAttribute("name", "submit_content");
Gumbo
  • 643,351
  • 109
  • 780
  • 844
8
document.getElementById("in1").setAttribute("name", "submit_content");

or using jQuery:

$("#in1").attr("name", "submit_content");
kim3er
  • 6,306
  • 4
  • 41
  • 69