tags:

views:

66

answers:

6

Hi I'm having some problems with jquery checkbox selected. I want to do a task when the checkbox is selected. my code for this is

$(document).ready(function() {
        if ($('#chxGetText').is(':checked')) {
            alert("OK");
        }
    });

<input id="chxGetText" type="checkbox" />

But nothing happen when I check the checkbox. Any ideas?

+2  A: 

You need to bind an event listener to the change event of the checkbox. Right now, you’re only checking if the checkbox is checked when the page loads, i.e. once.

$(function() {
 $('#chxGetText').change(function() {
  if ($(this).is(':checked')) {
   alert('OK');
  } else {
   alert('Not OK');
  };
 }).trigger('change'); // trigger it on page load as well
});

Note that you should probably trigger the event on page load as well, in case the user refreshes the page after checking the checkbox (hence the .trigger('change') in my code).

Mathias Bynens
Great code Mathias, I will be using the .trigger fosho on this other page i got
Dejan.S
+2  A: 

You were not assigning an event to the element, try this:

$(document).ready(function() {
  $('#chxGetText').click(function(){
    if ($(this).is(':checked')) {
     alert("OK");
    }
  });
});
Sarfraz
This will work, of course, but it’s probably a good idea to execute this check on page load as well: http://stackoverflow.com/questions/2643266/2643275#2643275
Mathias Bynens
Thanks for this worked great
Dejan.S
@Dejan.S: You are welcome ....... :)
Sarfraz
A: 

:checked is just a selector, it doesn't bind any event listeners. Do something like this:

$(document).ready(function() {
    $('#chxGetText').change( function (e) {
       if($(this).is(':checked')) {
          alert('Checked');
       }
       else {
          alert('Unchecked');
       }
    });
});

<input id="chxGetText" type="checkbox" />
PatrikAkerstrand
You’re using `function(e)` but you’re not using the `e`. Why not omit it?
Mathias Bynens
i used this one removed the e like Mathias Bynens comment on. Not that is bothers the code just becuase it's not used. Thanks
Dejan.S
@Mathias: I probably would in my own code unless I wanted to inspect or call methods on the event object. Just wanted to show that event handlers are passed an event object.
PatrikAkerstrand
A: 

You need to assign an event handler to the onclick event of the checkbox. Your current code only runs once, when the document is loaded and doesn't react to any state changes after that.

<input id="chxGetText" type="checkbox" />

function checkState(node) {
    if (node.checked) {
        alert("OK");
    }
}

$(document).ready(function() {
    $('#chxGetText').click(function() {
        // Bind checkState to click event
        checkState(this);
    }).each(function() {
        // Initial check on load
        checkState(this);
    });
});
nikc
using jquery and no obtrusive javascript?
Sarfraz
You're right, I should've (or at least could've) skipped jquery in this solution altogether. I was merely trying to demonstrate why the original solution wasn't working as expected.
nikc
@nikc: Err, no — you should’ve used unobtrusive JavaScript. jQuery or not.
Mathias Bynens
The inline event, yes. But my intentions to demonstrate the problem with the original solution still stands. Revised anyhow.
nikc
A: 

Try this:

    $('#chxGetText:checked').alert('OK');
gmunkhbaatarmn
There is no `jQuery#alert` method. Even if there was, this line of code would still be the equivalent of what he’s already doing — i.e. _not work_.
Mathias Bynens
A: 

You can use code like

jQuery("#chxGetText").click(function() {
    if ($(this).is(':checked')) {
        alert("#chxGetText is checked");
    }
    else {
        alert("#chxGetText is unchecked");
    }
});
Oleg