-2

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

  • 2
    You have a syntax error `getElementById.(Text1)` remove the `.` Also, you have to convert `.value` to a number – Konrad Aug 22 '22 at 21:49
  • @KonradLinkowski `parseInt(string)`. – Shmack Aug 22 '22 at 21:51
  • @Shmack `parseInt` without the second argument is unsafe. I would rather use `Number()` or just `+` operator. – Konrad Aug 22 '22 at 21:53
  • You have a few typos: You aren't declaring the variables **Text1** and **Text3** anywhere. So you either need to declare them OR just wrap them in quotes to access those fields. And remove the period after **getElementById** – imvain2 Aug 22 '22 at 21:56
  • @KonradLinkowski is unsafe the proper word for this use case? From what I find, its more inefficient than unsafe. – Shmack Aug 22 '22 at 21:59
  • I meant [this](https://stackoverflow.com/questions/6611824/why-do-we-need-to-use-radix-parameter-when-calling-parseint) – Konrad Aug 22 '22 at 22:07

2 Answers2

0

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>
Damian
  • 138
  • 6
0

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>