简体   繁体   中英

Dynamically creating input elements onclick

Hey guys so I am creating input elements within a div when I click on a specific radio button with a javascript function.

It works great but when I first load the form the input elements dont appear in the div because the radio button has not been "clicked yet" although one of the radio buttons is default checked.

Basically if its checked I want them to appear, so on body load I have to call a function that checks the radio button I want default and I have to manually display the corresponding input elements that would appear if I had clicked the radio button.

This seems like a dumb work around of what I am trying to achieve, any ideas?

 <body onload="startup()">
        <input type="radio" name="type" onclick="createInput()" id="testradio" value="test">test</input>

 <div id="area">
 </div>
</body>

function startup()
{
    document.getElementById("testradio").checked = true
    createInput();
}

function createInput()
{
    var testinput = "<input name='options' value='testing' type='checkbox'>test<br>"
    document.getElementById("area").innerHTML = testinput 
}

You should set the default value and then call the event. (say onchange)

something like this:

document.getElementById('elID').onchange();

this will then let your event listener fire as if the value where changed in the ui not in code.

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