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 ?

6
  • can you post the rendered HTML, I don't see a container #checkboxes there. Commented Aug 28, 2014 at 6:23
  • where id checkboxes exists ? Commented Aug 28, 2014 at 6:23
  • Sorry Guys I have added it now Commented Aug 28, 2014 at 6:25
  • If you have already chosen checked inputs, then what for you checked it again using if, also doing it in incorrect way? Commented Aug 28, 2014 at 6:26
  • @regent : i have used that code from stackoverflow.com/questions/2155622/… Commented Aug 28, 2014 at 6:27

5 Answers 5

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

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

1 Comment

That's I'm talking about :)
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'));
});

1 Comment

Even this works out as required ! Thanks exception for exceptional help :)
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);

1 Comment

The problem is in incorrect and unnecessary $(this).checked, not in inputs' selector.
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');
})

Comments

0

You could try this one

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

Comments

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.