Wednesday, 28 August 2013

passing array to with jQuery

passing array to with jQuery

I'm making a custom multiselect option box and I need to pass an array of
selected values to the element
var data=[];
$(".opt > div").click(function(){
data[data.length]=$(this).attr("value");
$(".options").val(data); // putting data into custom option
});
HTML
<div class="wrapper"> <!-- own custom style -->
<select class="options"> <!-- select is hidden with CSS -->
<!-- options -->
</select>
</div>
<div class="opt"> <!-- this div is dynamically created by clicking
no class wrapper and populated the inside with select box's options with
jQuery -->
<div value="0">item 1</div>
<div value="1">item 2</div>
<div value="2">item 3</div>
</div>
Everything is going well But on click event I want to value .option class
with array 'data' and pass it via It's form on submit as request. How to
set array (data) as It's value
Like in checkbox, giving name like name="somename[]" makes the response an
array.
the above code is just a brief demonstration of What I want

No comments:

Post a Comment