0

I have a change function in javascript who permit me to do an action when the user click on a chekbox who's name is "nameCheckbox" so i have this script.

$("input[name='nameCheckbox']").each(function()
{
     var object = creatObject($(this).val());
     $(this).change(function()
     {
           if($(this).is(':checked'))
           {
                 var object = creatObject($(this).val());
           }
           else
           {
                 object.remove();
           }
     });
});

But i want to have another checkbox who permit to check or uncheck all the checkbox who's name is "nameCheckbox" and pass by the function change of all the element. So i have this code.

$("[name='allCheck']").change(function()
{
        var selectAll = false;
        if($(this).is(":checked"))
        {
            selectAll = true;
        }

        $("input[name='nameCheckbox']").each(function()
        {
            if(selectAll)
            {
                 $(this).prop('checked',true);
            }
            else
            {
                 $(this).prop('checked',false);
            }
            //And here i want to do something like $(this).change();
        });           
    });

Thank you

5
  • And here i want to do something like $(this).change(); no problem with that: do it. Commented May 20, 2015 at 9:04
  • @VisioN, actually, I think you are right. $(this).change() should work Commented May 20, 2015 at 9:05
  • Why do you iterate over all the checkboxes in order to attach a change event? Do it in one line : $("input[name='nameCheckbox']").change(function(){... Commented May 20, 2015 at 9:05
  • Yes but it don't work if i do that it just doing nothing i want to launch the function change of the element. Commented May 20, 2015 at 9:05
  • @AmmarCSE Certainly. It will be the same as $(this).trigger('change'). Commented May 20, 2015 at 9:05

1 Answer 1

2

$(this).change() should work, according to change()

Description: Bind an event handler to the "change" JavaScript event, or trigger that event on an element.

This is effectively the same as using trigger()

$(this).trigger('change');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.