0

i am using this demo:

http://www.emblematiq.com/lab/niceforms/demo/v20/niceforms.html

i would like to know which values the user has selected.

<select size="4" name="languages[]" id="languages" multiple="multiple">

                    <option value="English">English</option>

                    <option value="French">French</option>

                    <option value="Spanish">Spanish</option>

                    <option value="Italian">Italian</option>

                    <option value="Chinese">Chinese</option>

                    <option value="Japanese">Japanese</option>

                    <option value="Russian">Russian</option>

                    <option value="Esperanto">Esperanto</option>

                </select>

the question is how do i return the values that were selected by the user?

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062

3 Answers3

1

Try something like this:

ob = document.getElementById('languages'); 

for (var i = 0; i < ob.options.length; i++){
    if (ob.options[ i ].selected){     
      alert(ob.options[ i ].value); //Do something useful here
     }
}
Eriksberger
  • 191
  • 4
1

I've put together a quick demo (using the select box structure from your example) here: http://jsfiddle.net/wp7sq/

So essentially, in my example I've created a simple function that gets which options the user selected, adds them to an array (just for convenience). So from here, you can output it to a string, or search your array, or do whatever you want using JavaScript using that array.

The relevant section is commented in the sample, and here's the jQuery code from my example:

var selectedLanguages = new Array();
jQuery('#languages option:selected').each(function() {
    selectedLanguages.push(jQuery(this).val());
});
Jared Cobb
  • 5,167
  • 3
  • 27
  • 36
0

$("#languages").value;should work

Leon
  • 2,810
  • 5
  • 21
  • 32
  • thanks so much can you show me how i could pass this into my webform? – Alex Gordon Sep 13 '11 at 22:23
  • you could do an ajax call, but as probably what you want to do, is change the language of the page, you should use a server control that performs a postback when the control change. By the way, in a language menu, you should use the localized name of the language, for example `` – vtortola Sep 13 '11 at 22:37
  • "languages" will not select anything (in the markup of the OP's example). And I don't think **.value** is a valid jQuery method. – Jared Cobb Sep 13 '11 at 22:43
  • With this kind of small tasks, I find it better to skip jQuery. Native is faster. If you have to write more lines of code with any framework than native, the alarm goes off in my head.. – Eriksberger Sep 13 '11 at 22:51