chino
chino

Reputation: 17

how to get value property from a checkbox

I need to get the value property of each checked checkbox from here:

$("input:checkbox[name=marca-chk]:checked").each(function(){        
    bufferTmp.push($(this).prop('value'));
});

but with .prop('value') I get the "is checked" value any idea to solve this?

Thank you in advance.

Upvotes: 1

Views: 243

Answers (3)

Tushar
Tushar

Reputation: 87203

From the comments by Op:

The problem is not with the prop(), but your <input> element does not have attribute value.

<input type="checkbox" name='marca-chk' val="on">marca-chk1
//                                      ^^^

So, you cannot get the property of name value, that'll be undefined.

Original Answer(Outdated)

Use val() instead of prop() to get the value of checkbox

Get the current value of the first element in the set of matched elements.

$(this).val()

Code

$("input:checkbox[name=marca-chk]:checked").each(function() {
    bufferTmp.push($(this).val());
    //                    ^^^^^^
});

Upvotes: 4

Sathish
Sathish

Reputation: 2460

You need to use .val() instead of .prop()

var bufferTmp = [];
$("input:checkbox[name=marca-chk]:checked").each(function(){        
    bufferTmp.push($(this).val());
  alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name='marca-chk' value="marca-chk" checked>marca-chk
<input type="checkbox" name='marca-chk1' value="marca-chk1">marca-chk1

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

You need to use $(this).val() instead of .prop() here.

$(this).val()

The .prop() is just for getting the properties of the DOM (eg. checked, and disabled).

The .val() is used for getting the current value of an <input> or <textarea> element.

Upvotes: 1

Related Questions