|
-
Feb 22nd, 2002, 07:36 AM
#1
Thread Starter
Evil Genius
Recognise 1st entry in combobox
Hi all! Any suggestions on this one please?
Code:
<HTML>
<HEAD>
<SCRIPT Language="javascript">
function whatSelected()
{
alert(document.frmSample.cmbBox1.value);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM Name="frmSample">
<SELECT Name="cmbBox1" Id="cmbBox1" onmouseup="whatSelected()">
<OPTION Value="1">1</OPTION>
<OPTION Value="2">2</OPTION>
<OPTION Value="3">3</OPTION>
<OPTION Value="4">4</OPTION>
<OPTION Value="5">5</OPTION>
</SELECT>
</FORM>
</BODY
</HTML>
If you look at this lil sample, it'll work if you select anything other than the first one (index 0 or option 1). I've tried onmouseup, onchange, selected property and can't for the life of me work out how to pick up the first entry.
-
Feb 22nd, 2002, 10:16 AM
#2
Fanatic Member
Yeah, I've notice that too!
I get over it by putting a dummy in first place, as it gets fired on the onchange event!
Code:
<HTML>
<HEAD>
<SCRIPT Language="javascript">
function whatSelected()
{
alert(document.frmSample.cmbBox1.value);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM Name="frmSample">
<SELECT Name="cmbBox1" Id="cmbBox1" onmouseup="whatSelected()">
<OPTION Value="0" selected>Please pick item!</OPTION>
<OPTION Value="1">1</OPTION>
<OPTION Value="2">2</OPTION>
<OPTION Value="3">3</OPTION>
<OPTION Value="4">4</OPTION>
<OPTION Value="5">5</OPTION>
</SELECT>
</FORM>
</BODY
</HTML>
-
Feb 22nd, 2002, 10:22 AM
#3
Thread Starter
Evil Genius
yeah, that one'd work!
Cheers Jerry
-
Feb 22nd, 2002, 12:21 PM
#4
Member
Hi,
I'm sorry to disagree, but...
'onmouseup' will always return a value of '0' primarily because that is where the mouse is initially 'downed'. You had best use 'onchange' as Jerry suggested.
Also, if you want to reuse the code elsewhere or by other selection lists, you would do well by passing the selected option to the function:
function doSel(selObj)
{
for (i = 0; i < selObj.length; i++)
if (selObj.options[i].selected)
alert(selObj.options[i].value)
}
<select name="cmbBox1" id="cmbBox1" onChange="doSel(this)">
Or, if the selected value is the only thing you really want:
function doSel(selVal)
{
alert(selVal)
}
<select name="cmbBox1" id="cmbBox1" onChange="doSel(this.options[this.selectedIndex].value)">
With the second example, you can do a few things, like change the location of the window. With the first one, you can do a few more things -- keep track of multiple selections, add/delete/move options, etc. If interested, see the selection list script/tutorials at my site.
Vinny
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|