How to Select Multi Select Picklist Value using Javascript</span>

Example to get the multi select picklist value using javascript

Example :1

<script language=”JavaScript”>
function getSelectedCountryValues() {
var dropDown = document.getElementById(‘countryValue’), countryValue = [], i;
for (i = 0; i < dropDown.options.length ; i += 1) {
if (dropDown.options[i].selected) {
//countryValue.push( dropDown.options[i].value); //If you only need values
countryValue.push({ Name: dropDown.options[i].text, Value: dropDown.options[i].value });
}
}
console.log(countryValue);
alert(‘countryValue’+ countryValue);
return false;
}
</script>

<form><label>Multi Select dropdown </label>
<select id=”countryValue” multiple=”multiple”>
<option value=”0″>Australia</option>
<option value=”0″>India</option>
<option value=”1″>United States</option>
<option value=”2″>United Kingdom</option>
<option value=”3″>China</option>
<option value=”4″>Bangladesh</option>
</select>

<input type=”submit” value=”Submit” /></form>

Example 2:

<select multiple=”multiple” name=”mySel”>
<option value=”val1″>option 1</option>
<option value=”val2″>option 2</option>
<option value=”val3″>option 3</option>
<option value=”val4″>option 4</option>
</select>
<button type=”button”>Show selected values</button>

</script><script type=”text/javascript”>

function showSelectsd(){
var selO = document.getElementsByName(‘mySel’)[0];
var selValues = [];
for(i=0; i < selO.length; i++){
if(selO.options[i].selected){
selValues.push(selO.options[i].value);
}
}
}
</script>

Written by 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply

Your email address will not be published. Required fields are marked *

*