Solved

Multi Select Option

Posted on 2013-06-28
3
527 Views
Last Modified: 2013-06-30
I have a SharePoint jquery form that I use in a CEWP and one of the elements on the form is a Multi Selecion element, which is a drop down list that allows for multiple selections.  Ex:

<select name="cars" multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

The element needs to be pre-selected with data from a SharePoint list.  I have a column called cars and the data is comma dilimited (Saab,Audi).

So, using spservices and jquery, how do I pre-select the element with "Saab" and "Audi"?

Thanks for any help or insight you can provide.
0
Comment
Question by:Isaac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 57

Expert Comment

by:Julian Hansen
ID: 39284401
$(function() {
	$('select[name="cars"] option[value="saab"], select[name="cars"] option[value="audi"]').attr('selected', 'selected');
});

Open in new window

0
 
LVL 5

Author Comment

by:Isaac
ID: 39287240
That didn't work...Here it is in jsfiddle:

http://jsfiddle.net/rXGmc/
0
 
LVL 57

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 39287854
Yes - it does work!

Your JSFiddle does not work because you did not do it right.

This is what you should have done.
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
	$('select[name="cars"] option[value="saab"], select[name="cars"] option[value="audi"]').attr('selected', 'selected');
});
</script>
<style type="text/css">
</style>
</head>
<body>
<select name="cars" multiple="multiple">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>
</body>
</html>

Open in new window


A link to a working version of the above code

Note you have to use the "cars" name for the select - not Audi or any other value.
You have to prefix all option selectors with the Select parent so as not to target all options on the page.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question