简体   繁体   中英

How to add attribute to HTML element using javascript

Using javascript (preferably not jquery) I'm trying to change the line:

<input type="number" name="price" required="" id="id_price">

As torazaburo suggests in the comment you can do it in one step with setAttribute() method

 document.getElementById("id_price").setAttribute("step","any");
 <input type="number" name="price" required="" id="id_price">


OR

First create the attribute and set the value. Then add it to the element..

 var attr = document.createAttribute('step'); attr.value="any"; document.getElementById("id_price").setAttributeNode(attr);
 <input type="number" name="price" required="" id="id_price">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM