DanielBoven
DanielBoven

Reputation: 53

Let checkbox be checked with delay

I have a simple question: How can you let a checkbox be checked automatically (as already shown in the code snippet), but with a delay of 100ms, so that when you load the page after 100ms, the checkbox gets checked (and will stay checked)? I think you need to use JavaScript for it, but I don't know how to use it.

<input checked type="checkbox"/>

Any ideas for a clean and good way to achieve this?

Upvotes: 1

Views: 2775

Answers (2)

user7929528
user7929528

Reputation:

Use setTimeout to count to 100ms. Then grab the checkbox with its id then use .checked = true.

setTimeout(function(){
document.getElementById('chech').checked = true;
},1000) // used 1 second to show the effect
<input id="chech" type="checkbox"/>

OR

setTimeout(function(){
document.getElementById('chech').click(); 
},1000) 
<input id="chech" type="checkbox"/>

Upvotes: 3

Anis Jonischkeit
Anis Jonischkeit

Reputation: 894

<input id='myInput' type="checkbox"/>
<script>
window.onload = () => { 
  setTimeout(() => {
    document.getElementById("myInput").setAttribute("checked", true);
  }, 100);
}
</script>

Upvotes: 1

Related Questions