Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to use javascript to insert an attribute to an element

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

like image 356
mark Avatar asked Jul 02 '09 09:07

mark


People also ask

Can JavaScript add HTML elements?

Using the innerHTML attribute: To append using the innerHTML attribute, first select the element (div) where you want to append the code. Then, add the code enclosed as strings using the += operator on innerHTML.

What is attribute in JavaScript with example?

attributes : a collection of objects that belong to a built-in Attr class, with name and value properties. HTML attributes have the following features: Their name is case-insensitive ( id is same as ID ). Their values are always strings.

Can I add any attributes to HTML elements?

We can create any custom attribute, by adding, data-, followed by the attribute name. Let's say, for example, we want to have a custom author attribute, which represents the author of a paragraph. We can do this with the following code shown below. Within the full HTML element, this would look like the following.


1 Answers

Try this:

document.getElementById("in1").setAttribute("name", "submit_content");
like image 77
Gumbo Avatar answered Nov 15 '22 18:11

Gumbo