0

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. Or an

the above code is just a brief demonstration of What I want

1
  • You should take the problem in reverse. Make your select with its options in the html. Then hide it with CSS, and dynamicly construct the "custom multiselect" with jQuery. Don't forget to add the multiple attribute to your select : <select multiple="multiple"> Commented Aug 28, 2013 at 12:34

3 Answers 3

1

First, make your select multi and give it a name:

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

Second, on the click, reset your select options and reselect select all that are in the data array.

var data=[];
$(".opt > div").click(function(){
   data[data.length]=$(this).attr('value');

   //Unselect all the options
   $('.options option').each(function(){ this.selected = false; })

   //For each item in data, select the respective option in the select
   $.each(data, function(){
      $('.options options[value="' + this + '"]').prop('selected', true);
   });
});

Finally, when you submits your form, the cmbSomeName will be an array in the server side

Its important to note that this code will always increase the data[] array, so, you will not be enable to "unselect" those itens, for such, I would do diferent coding like so:

 $(".opt > div").click(function(){
      jqOption = $('.options options[value="' + $(this).attr('value') + '"]');

      //Toggle (if true then false else true)
      jqOption.prop('selected', !jqOption.prop('selected'));
 })
Sign up to request clarification or add additional context in comments.

Comments

0

Use,

$(".options").append(data); 

Dont use value for writing html

3 Comments

I'm not writing HTML, I'm trying to value the <select> with jQuery as it naturally happens in normal <select> box
@Ario That's not how a <select> works. The value is contained in <option> elements in it.
But I made the same thing for a single value
0

DEMO

The trick is to:

  • Clone the select options
  • Put them inside a DIV (ok, who cares, it works beautifully)
  • target the original Select element on DIV click:

<div class="wrapper"> <!-- note! it wraps the select and the .opt -->
  <select class="options" multiple>
    <option value="0">Item 1</option>
    <option value="1">Item 2</option>
    <option value="2">Item 3</option>
  </select>
  <div class="opt"></div> <!-- will be populated by jQ -->
</div>

$('.options').each(function(){ 
  $(this).next('.opt').html($(this).html());
});

$('.opt').on('click', 'option', function(){
  $(this).toggleClass('selected')
         .parent().prev('select')
         .find('option[value='+ this.value +']')[0].selected ^= 1;
});

with this basic CSS:

.opt option{
  background:#eee;
  border-bottom:1px solid #888;
  cursor:pointer;
}
.opt option:hover{
  background:#ddd;
}
.opt option.selected{
  background:#cf5;
}

7 Comments

yes But I want to pass multiple values. Like value 0 and 1 together
@Ario : Do you mean 0 and item1 or (1 and item2 )?
I mean value 0 and 1 (of attribute data-value)
@Ario than you don't want a select dropdown :) cause that's not how <select> works... Or I understand your question totally wrong ...
@Ario you want actually a select multiple, right? Cause in yout HTML example you did not stated that it's a multiple as attribute
|

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.