How to detect checkbox being checked using javascript


jqueryDue to some limitation, i can’t use onClick javascript event to detect checkbox being checked. Alt solution to auto detect checkbox being checked in the page using jQuery javascript framework. I just embed few lines of javascript and it will auto detect all the checkbox status in a page. jQuery is too awesome. but too bad i dun really like Javascript, tat’s y i’m writing such an stupid easy tutorial for myself.

To detect checkbox being checked using jQuery javascript framework, follow the steps below

Advertisements

  • Load jQuery framework at the <head> of your page.
  • Embed the code below after your form and make sure you replace the ‘your_checkbox_name‘ below to your real checkbox name.
    $("input[type='checkbox']").click(function() {
      // if you want information about the specific checkbox that was clicked        
      if($(this).attr('name') == 'your_checkbox_name' && $(this).attr('checked') == true ) {
        // do sth if checkbox being tick
        alert('hello world - checked');
      } else {
        // do sth if checkbox being untick
        alert('hello world - unchecked');
      }
    });
    
  • Refresh your page and try to tick on your checkbox and it should show an alert now.



Share this with your friends:-

Leave a Reply