简体   繁体   中英

I'm trying to create a javascript Dom radio button and attribute dynamically

I'm trying to create a radio button using javascript. But it doesn't seem to work

function div()
{
    var newDiv = document.createElement("div");

    newDiv.id = "myDiv";
    newDiv.style.width = "200px";
    newDiv.style.height = "100px";
    newDiv.style.background = "red";
    newDiv.style.border = "inset";
    newDiv.style.borderwidth = "20px";
    newDiv.style.borderColor = "blue";

    document.body.appendChild(newDiv);
}

Change the div with input .And add type="radio"

 function div() { var newDiv = document.createElement("input"); newDiv.id = "myDiv"; newDiv.style.width = "200px"; newDiv.style.height = "100px"; newDiv.style.background = "red"; newDiv.style.border = "inset"; newDiv.style.borderwidth = "20px"; newDiv.style.borderColor = "blue"; newDiv.type="radio"; document.body.appendChild(newDiv); } div(); 

Updated

how to set the attribute dynamically? Its same as above . Dom element call via id

 function div() { // var newDiv = document.getElementById("input"); //its call the element //or var newDiv = document.getElementsByTagName("input")[0]; newDiv.id = "myDiv"; newDiv.style.width = "200px"; newDiv.style.height = "100px"; newDiv.style.background = "red"; newDiv.style.border = "inset"; newDiv.style.borderwidth = "20px"; newDiv.style.borderColor = "blue"; newDiv.type="radio"; document.body.appendChild(newDiv); console.log(newDiv.outerHTML) } div(); 
 <input id="input"> 

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