Pre-select multiple select option in jQuery
Description
The following code shows how to pre-select multiple select option.
Example
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){<!-- w ww.jav a2 s . c o m-->
$("#single").val("Single2");
$("#multiple").val(["Multiple2", "Multiple3"]);
$("input").val(["check1","check2", "radio1" ]);
});
</script>
<style>
.selected { color:red; }
.highlight { background:yellow; }
</style>
</head>
<body>
<body>
<select id="single">
<option>1</option>
<option>2</option>
</select>
<select id="multiple" multiple="multiple">
<option selected="selected">Multiple</option>
<option>Multiple2</option>
<option selected="selected">Multiple3</option>
</select><br/>
<input type="checkbox" name="checkboxname" value="check1"/> check1
<input type="checkbox" name="checkboxname" value="check2"/> check2
<input type="radio" name="r" value="radio1"/> radio1
<input type="radio" name="r" value="radio2"/> radio2
</body>
</html>
The code above generates the following result.
...
Create a button clone and insert it after t...
Disable buttons whose index is greater than...
Fire focus event in jQuery
Get input text from Text Box in jQuery
Get multiple selected value in jQuery
Get selected items from form select control...
Get single selected value from select in jQ...
Get text from button and set the value to i...
Get text value from a button and set it to ...
Get the input value of the first matched se...
Get value for each form element in jQuery
Handle focus event fired when an element re...
Handle form input Text box selected event i...
Handle form input Text input change event i...
Handle form inut key up event in jQuery
Hide all the input elements within a form i...
Invoke Submit Event in jQuery
Make a form input text element editable and...
Preselect checkbox in jQuery
Preset form radio button in jQuery
Select all selected items from form select ...
Select checkbox from a list of form control...
Select checked checkbox in jQuery
Select disabled button and get the its next...
Select form button and check element list l...
Select form file input field in jQuery
Select form image control in jQuery
Select form input text in jQuery
Select form password field in jQuery
Select form radio button in jQuery
Select hidden form fields in jQuery
Select reset button from form in jQuery
Select submit button in jQuery
Set focus to text box in jQuery
Set multiple value selected for select elem...
Set value for form input element in jQuery
...
Create a button clone and insert it after t...
Disable buttons whose index is greater than...
Fire focus event in jQuery
Get input text from Text Box in jQuery
Get multiple selected value in jQuery
Get selected items from form select control...
Get single selected value from select in jQ...
Get text from button and set the value to i...
Get text value from a button and set it to ...
Get the input value of the first matched se...
Get value for each form element in jQuery
Handle focus event fired when an element re...
Handle form input Text box selected event i...
Handle form input Text input change event i...
Handle form inut key up event in jQuery
Hide all the input elements within a form i...
Invoke Submit Event in jQuery
Make a form input text element editable and...
Pre-select multiple select option in jQuery
Pre-select options in form select element i...Preselect checkbox in jQuery
Preset form radio button in jQuery
Select all selected items from form select ...
Select checkbox from a list of form control...
Select checked checkbox in jQuery
Select disabled button and get the its next...
Select form button and check element list l...
Select form file input field in jQuery
Select form image control in jQuery
Select form input text in jQuery
Select form password field in jQuery
Select form radio button in jQuery
Select hidden form fields in jQuery
Select reset button from form in jQuery
Select submit button in jQuery
Set focus to text box in jQuery
Set multiple value selected for select elem...
Set value for form input element in jQuery
...