Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prototype - How to deselect the selected value from a dropdown

How do I deselect the selected value from a dropdown list using Prototype.

From

<select id=“mylist” MULTIPLE >
<option value=“val-1”>Value 1</option>
<option value=“val-2” SELECTED>Value 2</option>
<option value=“val-3”>Value 3</option>
</select>

To

<select id=“mylist” MULTIPLE >
<option value=“val-1”>Value 1</option>
<option value=“val-2”>Value 2</option>
<option value=“val-3”>Value 3</option>
</select>

Thanks for any help in advance.

like image 630
woot586 Avatar asked Jun 20 '11 16:06

woot586


2 Answers

I'm not sure how to do it in prototype, but I can do it in JavaScript.

For a regular select, set yourSelectElement.selectedIndex = -1.

For a multiple select, you can just ctrl+click on the selected item, but you can do it programmatically as well. See link.

http://jsfiddle.net/kaleb/WxJ9R/

like image 70
kzh Avatar answered Sep 23 '22 13:09

kzh


You can't : you'll have to add an empty option

<option></option> 

and then

$$("#mylist option[selected]")[0].selected = false;
$$("#mylist option")[0].selected = true;
like image 36
remi bourgarel Avatar answered Sep 25 '22 13:09

remi bourgarel