2

My form have following checkbox and I want to tick the option to show an alert with checked values, but there is nothing happen to show up the alert when I ticked it:

<form id="booking_form">
    <input type="checkbox" name="bType[]" id="bType" value="A,200">&nbsp;Type A - USD200<br>
    <input type="checkbox" name="bType[]" id="bType" value="B,150">&nbsp;Type B - USD150<br>
    <input type="checkbox" name="bType[]" id="bType" value="C,100">&nbsp;Type C - USD100<br>
    <input type="checkbox" name="bType[]" id="bType" value="D,50">&nbsp;Type D - USD50<br>

</form>

and I have JQ script:

<script>
$("input[type=checkbox]:checked").each ( function() {
    alert ( $(this).val() );
});
</script>

please advise, thanks.

3
  • 1
    What's wrong with Javascript? Commented Feb 4, 2013 at 3:21
  • 3
    you need to bind the change on checkbox Commented Feb 4, 2013 at 3:25
  • id of your checkboxes MUST BE UNIQUE Commented Oct 18, 2016 at 9:12

3 Answers 3

5

You have to add an event handler that initiates the code. Now it runs when the page is loading.

For instance, you can let a button click handle it:

$('.btn').click(function(){
    $("input[type=checkbox]:checked").each(function() {
        alert( $(this).val() );
    });
});

Here's an example: http://jsfiddle.net/tMRvL/

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

1 Comment

the original question ask to bind the event to the click, not to a button
2

That should works

  $("input[type=checkbox]").change( function() {
    if($(this).is(":checked")){
       alert( $(this).val() );
    }
  });

Demo on Jsfiddle, other demo on JSBin

Comments

0
<script>
    $("[type=checkbox]:checked").each ( function() {
        console.log($(this).val());
    });
</script>

Worked for me :)

1 Comment

don't forget to add some description/details while giving answer

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.