Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7942919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:07:13+00:00 2026-06-04T00:07:13+00:00

Consider the following code ( http://jsfiddle.net/FW36F/1/ ): <input type=checkbox onchange=alert(this.checked)> <button onclick=document.getElementsByTagName(‘input’)[0].checked=!document.getElementsByTagName(‘input’)[0].checked;>toggle</button> If you

  • 0

Consider the following code (http://jsfiddle.net/FW36F/1/):

<input type="checkbox" onchange="alert(this.checked)">
<button onclick="document.getElementsByTagName('input')[0].checked=!document.getElementsByTagName('input')[0].checked;">toggle</button>

If you click the checkbox, you get an alert telling you if it’s checked or not. Great. However, if you click the toggle button, the checkbox changes it’s checked state but the onchange event is NOT fired.

Essentially, the onchange for a checkbox only fires if the user actually clicks the checkbox, not if the checkbox is changed via JavaScript. This is be true in IE, FF, and Chrome. It appears that this behavior is to specification also.

However, I really need some kind of event to fire if, for any reason, the checkbox’s checked state changes. Is this possible?

Oh yeah, and jQuery is not allowed. And please no setTimeout/setInterval based solutions either…

Update: Also, I should make it clear that the code above is for illustration only. In the real code, we need to ensure the state of the checkbox is checked or unchecked — not just toggle it. Perhaps this would be better code to illustrate that:

<input type="checkbox" onchange="alert(this.checked)">
<button onclick="document.getElementsByTagName('input')[0].checked=true;">check</button> 
<button onclick="document.getElementsByTagName('input')[0].checked=false;">un check</button>

Moreover, there may be code in other areas we don’t fully control, which might do a simple .checked=true/false — we’d like to make sure we see that also.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:07:14+00:00Added an answer on June 4, 2026 at 12:07 am

    The existing answers work just fine, even with your update. Just be smart about it and don’t call click if you don’t need to. Also, please don’t use inline JS. That was OK 10 years ago.

    <input type="checkbox" onchange="alert(this.checked)">
    <button id='check'>check</button> 
    <button id='uncheck'>uncheck</button>
    
    document.getElementById('check').onclick = function() {
       if (!this.checked) {
          this.click();
       }
    }
    

    If you need to be modified when a script changes the value, in Firefox, you can use https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/watch

    Example here http://jsfiddle.net/PPuZ8/

    // In FF $ is a shortcut for document.getElementById
    // It doesn't fire when set from the UI, you have to use a regular handler for that
    $('cb').watch("checked", function(){
       console.log('Checked state changed from script', arguments);
       return true;
    });
    

    For IE you can use onpropertychange http://msdn.microsoft.com/en-us/library/ie/ms536956(v=vs.85).aspx (Thanks to jivings for the reminder)

    Example: http://jsfiddle.net/PPuZ8/1/

    document.getElementById('cb').onpropertychange = function() {    
        if (event.propertyName == 'checked') {
           console.log('Checked state changed onproperty change');    
        }
    };
    

    For other browsers, you have to poll using setInterval/setTimeout

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following html snippet <!DOCTYPE html> <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.4.2.js ></script> <script
Consider this following code (that retrieves the response from a HTTP request and prints
Consider following SWT code example: http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet151.java?view=co How can I separate the inline defined class?
Consider the following code: index.html <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01//EN http://www.w3.org/TR/html4/strict.dtd> <html> <head>
Consider the following code /* * To change this template, choose Tools | Templates
Consider the following example code: http://code.google.com/apis/maps/documentation/javascript/examples/streetview-simple.html I can do scrollwheel: false on a mapOptions
Consider the following code (C++11), which uses the Eigen 3 library (http://eigen.tuxfamily.org): #include <iostream>
This is rather interesting, I think. Consider following code, both the window.onload and body
Consider the following code: <html> <head></head> <body> <div id='test' class='blah'> <a href='http://somesite.com/' id='someLink'>click!</a> </div>
Consider the following code sending an HTTP 201 Created response to the client: String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.