最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

html - Change checkbox checked with JavaScript - Stack Overflow

matteradmin3PV0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 6

You 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>

Post a comment

comment list (0)

  1. No comments so far