admin管理员组文章数量:1341746
I am trying to change a part of my html code with JavaScript, but I can't get it to work:
function trigger(){
document.getElementById('mycheckbox').innerHTML="<input type="checkbox" id="mycheckbox" checked>";
}
function triggerOff(){
document.getElementById('mycheckbox').innerHTML="<input type="checkbox" id="mycheckbox">";
}
<input type="checkbox" id="mycheckbox">
<button type="button" onclick="trigger()">test</button>
<button type="button" onclick="triggerOff()">test</button>
I am trying to change a part of my html code with JavaScript, but I can't get it to work:
function trigger(){
document.getElementById('mycheckbox').innerHTML="<input type="checkbox" id="mycheckbox" checked>";
}
function triggerOff(){
document.getElementById('mycheckbox').innerHTML="<input type="checkbox" id="mycheckbox">";
}
<input type="checkbox" id="mycheckbox">
<button type="button" onclick="trigger()">test</button>
<button type="button" onclick="triggerOff()">test</button>
So if I press the button I want to add the checked status to my HTML and if I press the other button I want to remove the checked status. Is this even possible?
All help is highly appreciated ! Thanks a lot guys!
Share Improve this question edited Oct 24, 2018 at 14:03 Mamun 69k9 gold badges51 silver badges62 bronze badges asked Oct 24, 2018 at 13:18 pozapoza 2651 gold badge3 silver badges11 bronze badges3 Answers
Reset to default 6You cannot set innerHTML
to input type checkbox
. Use the checked
property to check/uncheck the checkbox
function trigger(){
document.getElementById('mycheckbox').checked = true;
}
function triggerOff(){
document.getElementById('mycheckbox').checked = false;
}
<input type="checkbox" id="mycheckbox">
<button type="button" onclick="trigger()">test</button>
<button type="button" onclick="triggerOff()">test</button>
Just do it like that
function trigger(){
document.getElementById('mycheckbox').checked = true;
}
function triggerOff(){
document.getElementById('mycheckbox').checked = false;
}
The innerHTML property will create inside your mycheckbox
another input
element
Use checked
property, like this:
function trigger(){
document.getElementById('mycheckbox').checked=true;
}
function triggerOff(){
document.getElementById('mycheckbox').checked=false;
}
<input type="checkbox" id="mycheckbox">
<button type="button" onclick="trigger()">test</button>
<button type="button" onclick="triggerOff()">test</button>
本文标签: htmlChange checkbox checked with JavaScriptStack Overflow
版权声明:本文标题:html - Change checkbox checked with JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743628563a2512713.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论