2

I am trying to find the easiest way to check change event checkboxes.

I created checkboxes with php script

PHP:

<div class="form-group">
    <div class="col-md-12 servicesCheck" style="margin-top:-10px">
        <?php
        foreach ($data["languageSchool"]["services"] as $service) {
            $required = "";
            $checked = "";
            if ($service["is_required"] == "1") {
                $required = "disabled";
                $checked = "checked";
            }
            echo "<div class='checkbox " . $required . "'>";
            echo "  <label><input id='services' type='checkbox' " . $checked . " " . $required . " value='" . $service["price"] . "'>" . $service["name"] . "</label>";
            echo "</div>";
        }
        ?> 
    </div> 
</div>

JQUERY:

$('#servicesCheck :checkbox').change(function () {
    // DO SOMETHING
});

What did I do wrong in my code.

2
  • What is servicesCheck? Commented Nov 12, 2016 at 19:32
  • @Xorifelse I confused class and id jquery selector. servicesCheck is class. Commented Nov 12, 2016 at 19:40

3 Answers 3

1

I think the jQuery selector is wrong. 'servicesCheck' in your HTML is a Class not an Id,

$('.servicesCheck input:checkbox').change(function () {
// DO SOMETHING
});

should work.

Although :checkbox works jQuery documentation says: "it is recommended to precede it with a tag name or some other selector"

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

Comments

0

your php code will generate multiple checkbox with same ID. change it by adding an auto increment suffix.. and also you used #servicesCheck in jQuery code, which is not available in this code

1 Comment

you right I just deal with fix it. echo " <label><input id='services" . $service["ID"] . "' type='checkbox'....
0

Personally I find it easier to specifically target by name attribute as it must exist in the source code as likely multiple check-boxes need a different handling and not the same one.

If you have only 1, Alvaro's answer will suffice.

$('#myloginform input[name=mycheckboxname]:checked').change(function(){
  console.log($(this).attr('name'));
});

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.