10

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
  • 43,322
  • 6
  • 62
  • 109
mark
  • 105
  • 1
  • 1
  • 6

2 Answers2

24

Try this:

document.getElementById("in1").setAttribute("name", "submit_content");
Gumbo
  • 620,600
  • 104
  • 758
  • 828
8
document.getElementById("in1").setAttribute("name", "submit_content");

or using jQuery:

$("#in1").attr("name", "submit_content");
kim3er
  • 6,170
  • 4
  • 39
  • 66