2

I am running this code and it returns me set of values which are checked

var a = [];
var cboxes = $('input[name="suppcheck[]"]:checked');
    var len = cboxes.length;
    for (var i=0; i<len; i++) {
        a[i] = cboxes[i].value;
       //document.getElementByName('suppgrp[]').value = a[i];

    }

I have a hidden field with ID suppgrp where I want to push all these values retrieved and wanted to pass it in array..

But I am not able to...where am I going wrong?

1
  • 2
    pass it outside the for loop Commented May 20, 2016 at 3:11

2 Answers 2

1

i have added some extra code which when page load then will call this function and also when change checkbox value then also call this function so all time it will work

loadCheck(); // initial call this function to load data

    $('input[type="checkbox"]').change(function()
    {
        loadCheck();

    });

    function loadCheck() {
        $('#hiddenValue').val('');
        $('#showValue').val('');
        var checkboxes = $('input[name="suppcheck[]"]:checked');
        var data = [];
        var len = checkboxes.length;
        for (var i=0; i<len; i++) {
            data[i] = checkboxes[i].value;

        }
        $('#hiddenValue').val(data);
         $('#showValue').val(data);
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
    <input type="checkbox" name="suppcheck[]" value="1" />
    <input type="checkbox" name="suppcheck[]" value="2" />
    <input type="checkbox" name="suppcheck[]" value="3" />
    <input type="checkbox" name="suppcheck[]" value="4" />
    <input type="checkbox" name="suppcheck[]" value="5" />
    <input type="checkbox" name="suppcheck[]" value="6" />
    <input type="checkbox" name="suppcheck[]" value="7" />
    <input type="checkbox" name="suppcheck[]" value="8" />
    <input type="checkbox" name="suppcheck[]" value="9" />

    <input type="hidden" id="hiddenValue" />
    <input type="text" id="showValue" />

</form>

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

Comments

0
var a = [];

jQuery('input[type="checkbox"]').change(function()   
{

 var cboxes = jQuery('input[name="suppcheck[]"]:checked');
 var len = cboxes.length;
 var alval = '';
 for (var i=0; i<len; i++) {
    a[i] = cboxes[i].value;
    if (alval != '') {
      alval += ','+a[i];
    }else{
      alval = a[i];
    }
 }

 jQuery('#myhidden').val(alval);

});

1 Comment

Please add some explanation to it

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.