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?

3 Answers 3

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());
});
Sign up to request clarification or add additional context in comments.

Comments

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
     }
}

Comments

0

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

4 Comments

thanks so much can you show me how i could pass this into my webform?
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 <option value="Spanish">Español</option>
"languages" will not select anything (in the markup of the OP's example). And I don't think .value is a valid jQuery method.
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..

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.