Results 1 to 4 of 4

Thread: Recognise 1st entry in combobox

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    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.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    Fanatic Member Jerry Grant's Avatar
    Join Date
    Jul 2000
    Location
    Dorset, UK
    Posts
    810
    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>
    Jerry Grant................tnarG yrreJ
    Website: <JG-Design></.net>
    Email: [email protected]
    Working towards a bug free world......
    (Not a Microsoft employee)

  3. #3

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    yeah, that one'd work!
    Cheers Jerry

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4
    Member Vincent Puglia's Avatar
    Join Date
    Feb 2002
    Location
    where the World once stood
    Posts
    36
    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
  •  



Click Here to Expand Forum to Full Width