Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove selection from a dropdown 1

Status
Not open for further replies.

mattquantic

Programmer
Mar 28, 2004
196
GB
Hi. I am tring to remove the selection from a dropdown, without having to repopulate it.

I'm trying with:

for (var n=0;n<document.addeditform[usefield].length;n++){
document.addeditform[usefield][n].selected = false;
}

It always leave one selected....

Any ideas?

Matt
 
is this kind of thing what you mean?
Code:
<script type="text/javascript">
function removeme(){
	var sb = document.getElementById("foo");
	var c = sb.selectedIndex;
	if (sb.length > 1) {
		sb.remove(c);
	} else {
		alert ("can't remove the last option");
	}
}
</script>

<select name="foo" id="foo">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
</select>
<button onclick="removeme()">Remove</button>
 
Sorry - I'd like to keep everything - but UN select them.

The solution I have in at the mo is a script that remove everything and then put's it back.

I'd like to return the dropdown to it's orginal state, with nothing selected, but clicking a button.

Matt
 
sorry to be dim but i am not sure what you mean by "unselected"

the relevant option will not appear "selected" when the focus moves away from it. i.e the white on blue (or whatever) disappears.

but an item always must be "selected" by a select box in terms of prescribing a value. if the user has not expressly changed the value in a select box then a properly designed browser should infer that the user wanted the item in index 0. so you can recreate this behaviour by just assigning the selectedIndex to 0
Code:
document.getElementById("foo").selectedIndex = 0;

what you cannot do is to "unselect" everything so that the browser does not send a result on form submission. you could, however, disable the control if this is what you want to achieve.
 
That's what I thought - but using
document.getElementById("foo").selectedIndex = 0;

selectes (blueback, white text) the first element.

So I suppose I'll have to dynamically remove all the elements and then add them again... :eek:(

Thanks for your time.

Matt
 
>So I suppose I'll have to dynamically remove all the elements and then add them again... :eek:(
Try this.
[tt] document.getElementById("foo").selectedIndex = [red]-1[/red];[/tt]
 
ACE.

I feel so dumb for not thinking of that.... duh.

Cheers!!!
 
selectes (blueback, white text)
this is to do with focus and not selection (at least on the browsers that i use [FF/IE]). if that is your concern why not just shift the focus to another element (which should be the case on pressing an unselect button anyway).
 
I am using two separate dropdowns for one form value - I won't go into it.

Sometimes it's multiple, sometime its single.

When its single, clicking on the otherside needs to de-select everything on the oposing side.

Thanks

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top