javascript - How would I check too see if "Checkbox" is checked,if so show element? -


<script> function validate(chk){ if (chk.checked == 1) document.getelementbyid('texttoshow').style.display="block"; else  alert("you didn't check it! let me check you.")  chk.checked = 1;  } </script>  <input type="text" value="hey" style="display:none"><input oncheck="validate(this.id)" type="check"> 

output: document.getelementbyid('texttoshow') null

this code check if checkbox checked , if so, show css hidden textbox. how make so in manner? 

there few things wrong here.

first lets fix html elements.

this needs id:

<input type="text" value="hey" style="display:none" id="texttoshow"> 

here need pass element instead of (non-existent) id. should onclick instead of oncheck

<input onclick="validate(this)" type="check"> 

if you're putting more 1 statement in if or else block, need contained withing curly brackets.

<script> function validate(chk) {     if (chk.checked == 1) document.getelementbyid('texttoshow').style.display = "block";     else {         alert("you didn't check it! let me check you.")         chk.checked = 1;     } } </script> 

i'd use true/false boolean statements 1 fine in js too.


Comments

Popular posts from this blog

Javascript line number mapping -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -