Ross Cooper
Ross Cooper

Reputation: 245

Detect check-box change

I have a list of check box items.

<ul class="level_1 ">
    <li class="level_1"><input type="checkbox" name="Settings"
        class="Settings" value="1"><label> Text 1</label></li>
    <li class="level_1"><input type="checkbox" name="Settings"
        class="Settings" value="2"><label> Text 2</label></li>
    <li class="level_1"><input type="checkbox" name="Settings"
        class="Settings" value="3"><label> Text 3</label></li>
</ul>

<div class="buttons">
    <input Value="Save" type="submit" id="settings_save_button"
        class="save yellow" name="settings_save_button"
        onclick="return false;" value="" />
</div>

bindData: function() {
    var self = this;
    $("#settings_save_button").click(function() {
        self.updateSettings();
    });
};

updateSettings: function () {
            var self = this;
            var ele = self.element;
            var formData = $(ele).find('form#edit_settings_form');
            $.ajax({
                type: "POST",
                url: formData.attr("action"),
                data: formData.serialize(),
                success: function (data) {
                    var userInfo = eval(data);
                    self.userRepo.updateUser(userInfo);
                    alert("saved");
                },
                error: function (req, textStatus, errorThrown) {
                    if (req.status == Portal.Constants.AJAX_VALIDATION_ERROR_CODE) {
                        self.addErrorMessageAndShowThem(eval("(" + req.responseText + ")"));
                    }
                },
                datatype: "json"
            });
        }

I now want to get rid of this save button. Instead I want the submit event to fire, every time user selects a new checkbox value. How can I bind the change in any checkbox with updateSettings method instead of click event of save button.

Upvotes: 0

Views: 57

Answers (3)

WickStargazer
WickStargazer

Reputation: 409

like this

$('input[name="Settings"]').bind('click',function(){

   //your code goes here
});

OR

$('input[name="Settings"]').on('click',function(){

   //your code goes here
   return true;
});

Cheers

Upvotes: 0

Sid
Sid

Reputation: 801

You have to bind that event to your check box

like this :

$(".Settings").on('change',function(e){
    e.updateSettings();
});

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

You can use the change event of the input element to bind a handler which will be called when the checkbox's checked state is changed

var self = this;
$('input[name="Settings"]').change(function () {
    self.updateSettings();
});

Upvotes: 4

Related Questions