0

Why is this not working? It is supposed to work this way: if the checkbox is checked, it should take the content of a variable txt and write it to the input with id of show.

function myFunction() {
  if (document.getElementById('myRadio').checked) {
    var txt = "Done";
    document.getElementById("show").innerHTML = 'txt';
  }
}
<p>If the radio checkbox is checked the text from the variable text is written into input line with the ID of show</p>
        
<input type="checkbox" onclick="myFunction()" id="myRadio">

<br>
<br>

<input type="text" class="form-control" id="show">

1
  • Don't you want to set the value of the text input? Commented May 18, 2022 at 16:29

1 Answer 1

2

You need to set the value property of the input element, not the innerHTML:

function myFunction() {
    if (document.getElementById('myRadio').checked){
        var txt = "Done";
        document.getElementById("show").value = txt;
    } else {
        document.getElementById("show").value = '';
    }
}
<p>If the radio checkbox is checked the text from the variable text is written into input line with the ID of show</p>

<input type="checkbox" onclick="myFunction()" id="myRadio">
<br>
<br>

<input type="text" class="form-control" id="show">
    

Sign up to request clarification or add additional context in comments.

1 Comment

Good practice would be to cache document.getElementById("show") so you don't have to specify it twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.