Select multiple values in a multiple select box with Jquery
Asked Answered
S

2

8

So I have two multiple select boxes like this

<select id="foo" multiple="multiple">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>

<select id="bar" multiple="multiple">
    <option value="1">Opt 1</option>
    <option value="2">Opt 2</option>
    <option value="3">Opt 3</option>
    <option value="4">Opt 4</option>
</select>
<a href="#" onclick="select()">Select</a>

What I'm trying to do is that when 'Select' is clicked, any option in "#bar" that has the same value with an option in "#foo" would be selected. In this case Opt 1 and Opt 2 in "#bar" should be selected. I've no idea why my javascript won't work. I know it must be something very simple. I just can't see it. :( So my Javascript function is as followed:

function select(){
    var vals = new Array();
    var iter = 0;
    $("#foo option").each(function(){
        var v = $(this).val();
        $('#bar option').each(function(){
            if ($(this).val() == v)
            {
                vals[iter] = v;
                iter++;
                break;
            }
        });
    });
    $("#bar").val(vals);
 }
Scoliosis answered 7/4, 2012 at 5:16 Comment(0)
L
5

UPDATE after seeing KON's example

DEMO

$("#sel").click(function(e) {
  e.preventDefault(); // cancel the link itself
  $("#bar").val($("#foo").val());
});

<a href="#" id="sel">Select</a>

Older example using each

DEMO

$("#sel").click(function(e) { // when link clicked
  e.preventDefault();
  $("#foo option:selected ").each(function(){
    var v = $(this).attr("value"); // first select's value
    $('#bar option').each(function(){
      if ($(this).attr("value") == v) { 
        $(this).attr("selected",true); // select if same value
      }
    });
  });
})
Lagging answered 7/4, 2012 at 5:53 Comment(0)
G
5

Check this out http://jsfiddle.net/NtF8J/

html

<select multiple>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
</select>

<select multiple>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
</select>

<button> random </button>​

jquery

$(function(){
  $(document.body).delegate(
    'select:first', 'change', function(){
      $('select:not(:first)').val(
        $(this).val()
      )
    }
  )
  .delegate(
    'button', 'click', function(){
     $('select').val([1,2,3,4].filter(function(){return!!Math.round(Math.random() * 1)}))
    }
  )
});



​
Gardel answered 7/4, 2012 at 5:41 Comment(5)
When posting your answers make sure you include your code. Fiddle's don't last forever and the goal of SO is to create a lasting repository of good questions with good answers. Links die. We want others to benefit from your knowledge.Smallminded
Apologies, didn't know they don't last forever.Gardel
No problem. I edited your question for you to include the code. +1 for a nicely coded answer.Smallminded
The asker is new at JS - I do not think your random selection generator with filter on hardcoded values is very explanatory or even answers the question. Why delegate by the way?Lagging
Still thanks for teaching me that jQuery can select multiple using just valLagging
L
5

UPDATE after seeing KON's example

DEMO

$("#sel").click(function(e) {
  e.preventDefault(); // cancel the link itself
  $("#bar").val($("#foo").val());
});

<a href="#" id="sel">Select</a>

Older example using each

DEMO

$("#sel").click(function(e) { // when link clicked
  e.preventDefault();
  $("#foo option:selected ").each(function(){
    var v = $(this).attr("value"); // first select's value
    $('#bar option').each(function(){
      if ($(this).attr("value") == v) { 
        $(this).attr("selected",true); // select if same value
      }
    });
  });
})
Lagging answered 7/4, 2012 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.