Reputation: 161
I have status id defined in my div tag which is present in a phtml file .Now, I need to fetch its value into the seprate javascript file.
I have use jquery push method for this.
Code in phtml file :
<table class="abc">
<tbody id="checkboxes">
<tr class="r<?php echo $item['id']; ?><?php echo ($item['status_low']?' status-low':''); ?>">
<td><input type="checkbox" name="item" class="row" statusid="<?php echo $item['status_id']; ?>"</td>
<td><?php echo $item['id']; ?></td>
<td><?php echo $item['title']; ?></td>
</tr>
</tbody>
</table>
Code in Javascript File :
var selected = [];
$('#checkboxes input:checked').each(function() {
if ($(this).checked) {
selected.push($(this).attr('statusid'));
}
});
console.log(selected);
When I print selected array, I get a blank output .
Can anyone tell me where am I going wrong ?
Upvotes: 0
Views: 819
Reputation: 556
You could try this one
var selected = $('#checkboxes input:checkbox:checked.row').map(function () {
var cbId = this.id;
return cbId;
}).get();
Upvotes: 0
Reputation: 18883
Try this :-
var selected = [];
$("input[type='checkbox']").each(function(){
if($(this).is(':checked'))
{
selected.push($(this).attr('statusid'));
}
});
OR
var selected = [];
$("#checkboxes input[type='checkbox']").each(function(){
if($(this).is(':checked'))
{
selected.push($(this).attr('statusid'));
}
});
OR
var selected = [];
$("#checkboxes input[type='checkbox']:checked").each(function(){
selected.push($(this).attr('statusid'));
});
Upvotes: 1
Reputation: 26342
"#checkboxes input:checked" selector already selecting checked checkboxes so you don't need to check if checkbox is checked or not. See here http://api.jquery.com/checked-selector/
var selected = $.map('#checkboxes input:checked',function(ck,i){
return $(ck).attr('statusid');
})
Upvotes: 0
Reputation: 25892
Just remove your if
condition like bellow. Your selector is just for selected checkboxes, So you don't need that if anyway.
$('#checkboxes input:checked').each(function() {
selected.push($(this).attr('statusid'));
});
Upvotes: 1
Reputation: 2276
Change your JQuery code like this,
$('#checkboxes input:checked')
to
$("#checkboxes input[type='checked']")
your code is:::
var selected = [];
$("#checkboxes input[type='checked']").each(function() {
selected.push($(this).attr('statusid'));
});
console.log(selected);
Upvotes: 0