0

I'm trying to get checkbox value from multiple checkbox.I have code as below:

 <input class="ng-pristine ng-valid" type="checkbox" name="ch" value="a" ng-model="todo.done">
 <input class="ng-pristine ng-valid" type="checkbox" name="ch" value="b" ng-model="todo.done">
 <input class="ng-pristine ng-valid" type="checkbox" name="ch" value="c" ng-model="todo.done">
 <input class="ng-pristine ng-valid" type="checkbox" name="ch" value="d" ng-model="todo.done">

what I want: when I check on checkbox,it will alert the value of checkbox. I have jquery code as below:

  $('input[type="checkbox"]').change(function() {
$('input[type="checkbox"]').each(function() {
    if ($(this).is(':checked')) {
    //the problem in here when I alert it will display many time with the other value of checkbox
    //what I need: I want to get only value of it when I check on it.
        alert($(this).prop('value'));
    }
});
});

Anyone please kindly help me, Thanks.

7 Answers 7

3

There is no need to loop through the checkbox elements

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

Demo: Fiddle

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

Comments

1

This should work like this

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

FIDDLE

1 Comment

$(this).attr("") and $(this).is("") can be this.
0

ok i know in php if you want to have the same name for you checkbox you need to put it in an array

<input class="ng-pristine ng-valid" type="checkbox" name="ch[1]" value="a" ng-model="todo.done">

etcetera, so im sure you have to loop through the array in jquery as well, sorry i dont konw the exact code off hand. :(

Comments

0

Try this:

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

Comments

0

try this,

$(this).val();

its working.

Comments

0

try the following code. in your $(this) refers to the elemnets of $('input[type="checkbox"]').each, hence it repets for all checkbox. I have changed change event to click as this one is good to use rather than change.

  $('input[type="checkbox"]').click(function() {

    if ($(this).is(':checked')) {
               alert($(this).val());
    }

 });

Comments

0

Look more into each jQuery function... I think what you need is parameters inside the each function like each(function(i,element) {}) where i behaves as the iterator for the checkboxes and element can be used to reference that checkbox. I think the issue is because you're using (this), which is not referencing the exact checkbox.

I hope I led you in the right direction.

7 Comments

if you include more then i led you in the right direction, i will +1.
ok...then you will be able to do almost every kind of thing related to the checkbox within the DOM structure....using parent and siblings.. for simple alert do alert(element.val())
i meant explain your answer better, and also add something to help the "asker" in his specific case.
although really the loop is unnecessary.
ok, my first post in stackoverflow didn't go well....loop will allow for more flexible approach if you would require to add other stuffs later.
|

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.