0

Currently, I have multiple checkbox inputs with different names(checkit, checktype, checklog) assigned to the inputs. What I want to do is to have each checkbox to change the color of the background when checked. However, I dont know how I can assign each one of the checkbox to do some tasks without duplicating the following code ?If possible some examples or tips will be great! I would love to hear from you .

Should I remove name="checkit" if I want to make all the inputs do the same thing? What if I want them to do some slightly different things?

$('input[name="checkit"]').change(function () {
        if ($(this).prop('checked')) {
            $(this).parent().parent().addClass('alterBackground');
        } else {
            $(this).parent().parent().removeClass('alterBackground');
        }
    });
1
  • Add a class to each and refer to it instead of looking for the name Commented Oct 11, 2017 at 6:50

5 Answers 5

1

Add the following by , or give some class name to it

$('input[name="checkit"], input[name="checktype"], input[name="checklog"]').change(function () {
    if ($(this).prop('checked')) {
        $(this).parent().parent().addClass('alterBackground');
    } else {
        $(this).parent().parent().removeClass('alterBackground');
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

Don't use the name atrribute in jQuery and add a common class to each checkbox for a common functionality and access it with class selector in jQuery as shown below.

If you want to do something different with different checkboxes apart from this, then you can add more jQuery code for that specific input tag. It will not affect this code.

$('input.someClass').change(function () {
        if ($(this).prop('checked')) {
            $(this).parent().parent().addClass('alterBackground');
        } else {
            $(this).parent().parent().removeClass('alterBackground');
        }
});

Comments

1

You can remove the name part from the selector and add selector for input[type='radio']. And if you want to add a bit different logic (I think you mean different classes), you can get the name of the current checked checkbox and use it to make your logic. Something like this

$('input[type="radio"]').change(function () {
     var checkboxName = $(this).prop('name');

     // if(checkboxName === .....)

});

Updated according to the comment

$('input[name="checkit"], input[name="checktype"], input[name="checklog"]').change(function () {
    var checkboxName = $(this).prop('name');

    // .............
});

3 Comments

What if I want only the inputs with a certain name do something? How will it be possible?
You need to use separator selector like $('input[name="1"], input[name="2"]'). This returns only elements with given selectors
Make it as a class. For example <input type="radio" name="checkit" class="checkit"> and you can refer in jquery as $('input.checkit)
0
$('input[type="checkbox"]').change(function () {
        if ($(this).prop('checked')) {
            $(this).parent().parent().addClass('alterBackground');
        } else {
            $(this).parent().parent().removeClass('alterBackground');
        }
    });

Comments

0

Use

$('input[type="checkbox"]')

instead of

$('input[name="checkit"]')

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.