i am have put a loop in java script and within the loop , i create the drop down repeatedly by calling a div within which my codeigniter form helper dropdown is present, something like this
for(){
var demo = $('<select>').attr('name','id_'+i).html($('#demo_id').html());
}
My demo_id is like this:
<div style="display:none;">
<?php echo form_dropdown('sample', $my_var, '', 'id="demo_id"') ?>
</div>
Now i want that , for each time the javascript for-loop runs , i want each drop down to select a particular value that i provide, something like
var demo = $('<select>').attr('name','id_'+i).attr('value',2).html($('#demo_id').html());
but this just sets value of select to 2 , does not populate the drop down with 2 selected
What can i do ?
Note: there is no issue in displaying the dropdown , my javascript is creating the dropdowns perfectly , ie same nos of times the for loop runs , just that i want to know how to populate the values
You should try the .val() function, not the attr, like this:
var demo = $('<select>').attr('name','id_'+i).html($('#demo_id').html()).val(2);
And I think, your version didn't work, because first you create the select element, then you gave a name attribute and a value, but at this point, you don't have any element inside select.