0

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 ?

mmt
  • 161
  • 1
  • 2
  • 13

5 Answers5

1

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'));
});
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
1

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'));
});

DEMO

Mritunjay
  • 25,338
  • 7
  • 55
  • 68
0

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);
chandu
  • 2,276
  • 3
  • 20
  • 35
0

"#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');
})
A.T.
  • 24,694
  • 8
  • 47
  • 65
0

You could try this one

 var selected = $('#checkboxes input:checkbox:checked.row').map(function () {
             var cbId = this.id;
             return cbId;
         }).get();
xei2k
  • 536
  • 4
  • 9