1

I have this coding on a check box:

<input type="checkbox" name="ECVValve" id="ECVValve" onChange="setEcvcheck();">

The check box can also be checked by another function (activated under certain conditions) that looks like this.

function setEcvcheck() {
        if (document.getElementById("ECVValve").checked) {
              document.getElementById("ECVValve").checked = false;
        } else {            
              document.getElementById("ECVValve").checked = true;
        }      
  }

It appears that if the checkbox is "checked" by the function setEcvcheck, the onChange event is not fired. Is that how it works or am I missing something else?

5
  • Where is your onchange declaration? Share it please. Commented Jan 15, 2016 at 13:02
  • You changed the onchange attribute, in your first edit the function that points was different. Your declaration should work perfectly with the code you share right now. Commented Jan 15, 2016 at 13:04
  • tried writing onchange with a small c? Commented Jan 15, 2016 at 13:05
  • @MarcosPérezGude Yes it worked. Commented Jan 15, 2016 at 13:05
  • However, your internal code makes no sense for me. When your checkbox change, if it's checked you uncheck it, and viceversa ? What sense have this behaviour? Commented Jan 15, 2016 at 13:06

3 Answers 3

2

I think there is a loop of checking and un-checking in the code.. because each time you change it to true, it will call the onChange, then it will come back to function and set it to false. Are you trying to check and un-check the same checkbox? (ECVValve)?

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

Comments

1

Actually, the event is firing. When you click, you check it, your javascript code unchecks it. And you'll never get to check the checkbox. You don't need to write javascript code to check or uncheck. To be sure it's firing, try this:

function setEcvcheck() {
    alert('Ok!');
}

Comments

1

you can programatically fire the onchange event of check box.

in the following code, when button clicked toggle the check box and force to fire the onChange event of it.

function test() {
  console.log("test");
}

function btnclick() {
  var x = document.getElementById("1");
  x.checked = !x.checked;
  document.getElementById("1").onchange()
}
<input type="checkbox" onChange="test()" id="1">hi

<button onClick="btnclick()">btn</button>

Comments

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.