get each child('id') into array or string

Daniel Hunter picture Daniel Hunter · Mar 15, 2012 · Viewed 26.4k times · Source

I need to create a comma separated value for a hidden input created from a series of div id's

<div id="main">
<div id="A"></div>
<div id="B"></div>
<div id="C"></div>
</div>

<input type="hidden" id="children"/>

I want to use jquery but having trouble with the function

    function update_input(main){
        var array = new Array();

           $('#'+main).children('id').each(function(){
                 array.push($(this).attr('div'));  
           });         

        input_value = array.toString();
        $('#children').val(input_value);
    }

This is not right

Answer

Frenchi In LA picture Frenchi In LA · Mar 15, 2012
    $('div','#main').each(function(){
      array.push($(this).attr('id')); 
    });