html - passing array to <option> with jQuery -


i'm making custom multiselect option box , need pass array of selected values element

var data=[]; $(".opt > div").click(function(){   data[data.length]=$(this).attr("value");   $(".options").val(data);  // putting data custom option }); 

html

<div class="wrapper"> <!-- own custom style -->   <select class="options"> <!-- select hidden css -->     <!-- options -->   </select> </div>  <div class="opt"> <!-- div dynamically created clicking         no class wrapper , populated inside select box's options jquery -->   <div value="0">item 1</div>   <div value="1">item 2</div>    <div value="2">item 3</div>  </div> 

everything going on click event want value .option class array 'data' , pass via it's form on submit request. how set array (data) it's value?

like in checkbox, giving name name="somename[]" makes response array. or

the above code brief demonstration of want

first, make select multi , give name:

<select name="cmbsomename" class="options" multiple="multiple"> 

second, on click, reset select options , reselect select in data array.

var data=[]; $(".opt > div").click(function(){    data[data.length]=$(this).attr('value');     //unselect options    $('.options option').each(function(){ this.selected = false; })     //for each item in data, select respective option in select    $.each(data, function(){       $('.options options[value="' + + '"]').prop('selected', true);    }); }); 

finally, when submits form, cmbsomename array in server side

its important note code increase data[] array, so, not enable "unselect" itens, such, diferent coding so:

 $(".opt > div").click(function(){       jqoption = $('.options options[value="' + $(this).attr('value') + '"]');        //toggle (if true false else true)       jqoption.prop('selected', !jqoption.prop('selected'));  }) 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -