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