Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Multi Select Option

Posted on 2013-06-28
3
Medium Priority
?
532 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 59

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 59

Accepted Solution

by:
Julian Hansen earned 2000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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)

705 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