0

I am trying to store all the checked checkboxes class names in an array to parse it using an ajax function later.

But first to make sure that all the classes being stored in the array correctly I alert them, but know, what I see is just the first checked checkbox class

there supposed to be many as the check boxes are generated dynamically .

Jquery

$(document).ready(function() {
    $('#actselect').change(function() {
      var conceptName = $( "#actselect option:selected" ).attr ( "id");
      if (conceptName == "payall"){
     var checkedB = new Array();
         checkedB.push($("input.NP:checkbox:checked").attr("class"));
     alert(checkedB);
  }

});

HTML

   a php while loop {

    <input type="checkbox"  value="None" id="itemselectNP" class="NP T<?php echo $productId; ?>" name="itemselect" />

   }

Please let me what is wrong with my code? I have tried different solutions poped on google search but still no luck.

Appreciated

4 Answers 4

1

use each() to trace multiple element right now it pick only one checkbox so use each to store all of their class

$(document).ready(function() {
    $('#actselect').change(function() {
      var conceptName = $( "#actselect option:selected" ).attr ( "id");
      if (conceptName == "payall"){
     var checkedB = new Array();
         $("input.NP:checkbox:checked").each(function(){
           checkedB.push($(this).attr("class"));
         });
     alert(checkedB);
  }

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

2 Comments

rattan your answer is so simple and so workable, thanks a lot, It will be appreciated if you could vote up my questions too. :)
someone down voted me again for no reason, could you help me out here?
0

Try this

var $array = [];

$.each($('input[type= checkbox]:checked'),function(i,val){
    $array.push('your want to push the value');
});

Comments

0

try this way

    var checkedB = new Array();
    $("input :checkbox:checked").each(function (){
           checkedB.push($(this).attr('class'));
    }) ;
    alert(checkedB);

Happy Coding:)

Comments

0

<script type='text/javascript'>
      $(document).ready(function() {

      GetSelectedCheckbox();
    $('#btnGet').click(function(){
        GetSelectedCheckbox();
    });
    $('#btnSave').click(function() {
            addCheckbox($('#txtName').val());
        });
    });
    function GetSelectedCheckbox()
    {
    var checkedB = new Array();
    $('input[type=checkbox]').each(function () {
        if(this.checked)
        {
            //alert($(this).attr('class'));
            checkedB.push($(this).attr("class"));
        }

     });
    alert(checkedB);
    }
    function addCheckbox(name) {
       var container = $('#checkDiv');
       var inputs = container.find('input');
       var id = inputs.length+1;

       $('<input />', { type: 'checkbox', id: 'cb'+id, value: name,class:'class'+id }).appendTo(container);
       $('<label />', { 'for': 'cb'+id, text: name }).appendTo(container);
    }
      </script>

http://jsfiddle.net/u8sRb/

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.