Solved

Multi Select Option

Posted on 2013-06-28
3
495 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
  • 2
3 Comments
 
LVL 51

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 51

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now