skip to Main Content

I’m trying to show an image when the checkbox is checked and show a text when it is unchecked.
When I check the checkbox I get the image but when I uncheck it the text doesn’t show.

<input type="checkbox" id="checkword" onchange="tran()">
<label for="checkword" class="word" id="word"></label>
let wordel = document.getElementById('word');
wordel.innerHTML = "A";
function tran() {
    if(wordel.checked != false){
        wordel.innerHTML = "";
        wordel.innerHTML = '<img src="001.gif">';
        wordel.style.padding = "0px";
        wordel.style.background = "none";
    } else {
        wordel.innerHTML = "A";
    }
}

1

Answers


  1. Posting as an answer because I can’t comment. It looks like you’re checking the wrong element is checked.

    You are checking that wordel is checked, but that’s the label element.

    You want to check:

    document.getElementById("checkword").checked
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search