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
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.
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.
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.
Try this:
document.getElementById("in1").setAttribute("name", "submit_content");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With