By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,543 Members | 2,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,543 IT Pros & Developers. It's quick & easy.

change multiple in single select

P: n/a
Hi all

how can i manipulate a multiple select into a single select dropdown
field with JavaScript?

thanks

Aug 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
document.forms["my_form"]["my_select"].multiple=false;

Aug 19 '05 #2

P: n/a
On 19 Aug 2005 12:39:46 -0700, d.********@gmx.net wrote:
how can i manipulate a multiple select into a single select dropdown
field with JavaScript?


<form name="myForm">
<input type="button" name="myButton" value="Toggle"
onclick="toggle();">
<br>
<select id="myMultiSelectId" name="myMultiSelect" multiple="multiple">
<option>One</option>
<option>Two</option>
<option>Three</option>
<option>Four</option>
</select>
<select id="mySelectId" name="mySelect" style="display:none;">
<option>One</option>
<option>Two</option>
<option>Three</option>
<option>Four</option>
</select>
</form>
<script type="text/javascript">
function toggle() {
if (document.getElementById) {
var myMultiSel = document.getElementById('myMultiSelectId');
var mySel = document.getElementById('mySelectId');
if (myMultiSel &&
(myMultiSel = myMultiSel.style) &&
mySel &&
(mySel = mySel.style)) {

myMultiSel.display = ('none' == myMultiSel.display ? '' :
'none');
mySel.display = ('none' == myMultiSel.display ? '' :
'none');
}
}
}
</script>

It's sort of fragile, if both select elements somehow ended up in the
same state, clicking the "Toggle" button wouldn't toggle them, it
would turn them both on/off.

The other thing you may want to consider is constructing a new select
using document.createElement() rather than swapping two select
elements on the page (especially if the select contains lots of
options and you don't want to replicate them twice).

--
Grant Wagner <sq*******@yahoo.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Aug 19 '05 #3

P: n/a
On 19 Aug 2005 13:41:11 -0700, "Markis Taylor" <do*********@gmail.com>
wrote:
document.forms["my_form"]["my_select"].multiple=false;


<form name="myForm">
<input type="button" name="myButton" value="Toggle"
onclick="toggle();">
<br>
<select name="myMultiSelect" multiple="multiple">
<option>One</option>
<option>Two</option>
<option>Three</option>
<option>Four</option>
</select>
</form>
<script type="text/javascript">
function toggle() {
document.forms['myForm'].elements['myMultiSelect'].multiple =
!document.forms['myForm'].elements['myMultiSelect'].multiple;
}
</script>

Results in really odd behaviour in Opera 8.02 and 7.54u2. You get a
single item select, but it has up/down arrow buttons instead of being
a pull-down menu.

--
Grant Wagner <sq*******@yahoo.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Aug 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.