Home > database >  If statement inside function on Javascript
If statement inside function on Javascript

Time:08-24

I cant find the issue here also as I want to display text and other text if the check box is checked

<script>

function showconf() {

var checkbox = document.getElementById('check');

if (checkBox.checked == true){
 document.getElementById('show').value = "test" ;
 
 }
 
 else {
 document.getElementById('show').value = "hello"  ;
 
 }
 
 }
 
 </script>

 
 <body>
 
 <input type="checkbox" id="check" onclick="showconf()" >
 <textarea id="show" name="message" rows="30" cols="100"></textarea>
 
 </body>

thanks in advance

CodePudding user response:

You are doing many syntax errors. first there are so many dots (.) in your document.getElementById() remove that and the value of document.getElementById(Text1) should be document.getElementById('Text1') between quotes ' or double quotes '. I have fixed everything now it will work good. Below is your fixed code.

<script>
  function hello (){
    var x = document.getElementById('Text1').value;
    if (x>10){
      document.getElementById('Text3').value = "1";
    } else {
      document.getElementById('Text3').value = "2";
    }
  }
</script> 

<body>
  <input type="text" id="Text1" />
  <br/>
  <input type="button" onclick="hello()"  value="Click..."/>
  <input type="text" id="Text3"/>
</body>

CodePudding user response:

If your are referencing DOM elements :

  • remember to use " in .getElementById : getElementById("Text1") Try this:

<script>
  function hello (){
    var x = document.getElementById("Text1").value;
    if (x>10){
        document.getElementById("Text3").value= "1";
    }
    else {
        document.getElementById("Text3").value= "2";
    }
}
</script>





<body>

  <input type="text" id="Text1" />

  <br/>
  <input type="button" onclick="hello()"  value="Click..."/>

  <input type="text" id="Text3"/>

</body>

  • Related