1

I have this code:

$('#chkBox').click(function() {
    $('#hiddenBox').val($('input').is(':checked'));
});

I want it to be where if i click on the checkbox, in my hidden field, I set the value to true else if it's not clicked, the value would be false. Can anyone help?

1 Answer 1

5
$('#chkBox').click(function() {
    // I prefer assigning a string
    $('#hiddenBox').val(this.checked ? 'true' : 'false');

    // but in actual fact, this should be enough
    $('#hiddenBox').val(this.checked);
}).triggerHandler('click');​​​

Demo: http://jsfiddle.net/pGkGz/1/

And see http://api.jquery.com/triggerHandler/

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

2 Comments

@ karim79 -- this will give me true if i check the checkbox but i want it where if it's not checked, i would get false, more like false on page load until i click the checkbox.
@hersh - Updated. You need to trigger the click handler on page load, using .triggerHandler.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.