Click to See Complete Forum and Search --> : Unselect value from a listbox
stefanb
04-09-2003, 12:35 AM
Hi!
Does anyone know how to unselect a selected value from a listbox by using JavaScript?
Ex:
I have a textbox (<input type=text name=textbox>) and a listbox (<select name=listbox>) on a page.
When I click (select) a value in the listbox the textbox is cleared: onclick="document.myform.textbox.value='';"
That works fine...
But I want the opposite function too... i.e. when I click on the textbox, then any selected value in the listbox shall be unselected.
A guy suggested this function:
function clrList() {
var i;
for (i=0;i<document.myform.listbox.length;i++) {
document.myform.listbox.options[i].checked=false;
}
}
...but that doesn't seem to work...
Any suggestions?
SniperX
04-09-2003, 01:28 AM
I was just wondering what your reasoning behind your uncheck uncheck on check, it doesn't make any sense to me, perhaps if you could give specs on this then maybe i could give a bit more help.
The code your friend gave you basically just initializes all the values to false and none of them are checked
Kind regards
MW
p.s.
Please do not take any offence to the above comments, i just want u to ask the question in a more detailed way, so i know exactly what you need it for
stefanb
04-09-2003, 03:08 AM
My intentions were basically what I wrote... if the user clicks in the textbox then the selected value (if any) in the listbox should be unselected... hmmm... call it an "aesthetic thing"...
SniperX
04-09-2003, 03:16 AM
try this: i actually dont know if it will work
onclick="myFunction('currentItem')"
<script>
<!--
function myFunction(obj) {
document.myform.listbox.obj.checked=false;
}
-->
</script>
That might be what you are looking for...
Please keep me posted
MW
edited coz i didnt read question properly..
ignore above and try this:
onclick="myFunction()"
<script>
<!--
var i;
function myFunction() {
i = check();
document.myform.listbox.obj[i].checked=false;
}
function check() {
for (var j=0; j<numObjects; j++)
if (document.myform.listbox.obj[j].checked == true) {
i = j;
}
}
-->
</script>
that should work
Regards MW
stefanb
04-09-2003, 04:43 AM
Thanks!
I will try that later...
I've made up a "dummy-page" if you have time to take a look at it: http://online.kunskapenshus.com/test.htm
Might explain a little bit better...
/ Stefan
SniperX
04-09-2003, 05:17 AM
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<h1 style="{font-family:verdana;font-size:12pt;}">JavaScript problem</h1>
<form name="myform">
<p>
<font style="{font-family:verdana;font-size:10pt;}">
First select a value from the list.<br>
</font>
<select name="listbox" size="6" onclick="{document.myform.textbox.value=document.myform.listbox.value;'';}" style="{font-family:verdana;font-size:8pt;width=300px;color:red;}">
<option value="1">Red</option>
<option value="2">Blue</option>
<option value="3">Green</option>
<option value="4">Yellow</option>
<option value="5">Orange</option>
<option value="6">White</option>
<option value="7">Black</option>
<option value="8">Purple</option>
<option value="9">Gray</option>
</select>
</p>
<p>
<font style="{font-family:verdana;font-size:10pt;}">
Then click in the textbox below.<br>
</font>
<input type="text" name="textbox" onclick="window.myform.listbox.value = 0;" style="{font-family:verdana;font-size:8pt;width=300px;color:red;}">
</p>
</form>
</BODY>
</HTML>
Replace your code with the code above...
It works on my pc.
NOTE: onclick="window.myform.listbox.value=0" the zero is there because no value is equal to that therefore it "focuses" on the listbox item with a value of zero and thus focus is lost
Hope it helps
Regards M W
stefanb
04-09-2003, 05:25 AM
Excellent!!!
It works! Thanks for all help!
SniperX
04-09-2003, 05:36 AM
Just glad i could help