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

2 Answers 2

28

Try this:

document.getElementById("in1").setAttribute("name", "submit_content");
Sign up to request clarification or add additional context in comments.

2 Comments

It’s actually setAttribute instead of addAttribute. I fixed that.
This one is not working.. what's the solution to this..?? setAttribute("event.stopPropagation()")
8
document.getElementById("in1").setAttribute("name", "submit_content");

or using jQuery:

$("#in1").attr("name", "submit_content");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.