Due 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.
Related posts:
Javascript: open new window with window.open()
jQuery: How to refresh select box
WordPress: How to open rss links in new window?
Easy to use jQuery Lightbox - Fancybox
How to delete table row using jQuery
Javascript: Prevent page from frameset hijack
jQuery: disable autoscrolling to top when click on anchor
How to validate drop down list in Javascript?
Share this with your friends:-