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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Event change of selectedIndex works for numbers but not words?

Status
Not open for further replies.

starblood

Technical User
Feb 28, 2001
63
GB
The sample below works - please note the bold OnClick event.

I cannot seem to get the the SelectedValue to change to 'NumberOne' with an onClick event? (possibly a syntax question because substituting 'NumberOne' for '1' in the line doesn't work)

Thanks in advance for your help!


<form name="TestForm" method="post" action="">
<select name="TestSelect">
<option value="select..." selected>Select...</option>
<option value="1">1</option>
<option value="NumberOne">NumberOne</option>

</select>
</form>
<a href="#" onClick="document.TestForm.TestSelect.selectedIndex = '1';">test</a>

 
Sorry everyone - I had a 'wood for the trees' moment. I have now sussed that I needed to use:

<a href="#" onClick="document.TestForm.TestSelect.value = 'numberone';">test</a>
 
I'm not sure you're getting the value to change, are you sure it's working? I tried your script and put in a test alert the value was "".
Code:
<form name="TestForm" method="post" action="">
  <select name="TestSelect">
    <option value="select..." selected>Select...</option>
    <option value="1">1</option>
    <option value="NumberOne">NumberOne</option>
    
</select>
</form>
<a href="#" onClick="document.TestForm.TestSelect.value = 'numberone';alert(document.TestForm.TestSelect.value)">test</a>
Working on a solution now.

Glen
 
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">

<html>
<head>
	<title></title>
    <script>
    function getIt(){
    var aa = document.TestForm.TestSelect.SelectIndex;
   
    alert(document.TestForm.TestSelect.value);
    }
    </script>
</head>

<body>

<form name="TestForm" method="post" action="">
  <select name="TestSelect" onchange="getIt()">
    <option value="Zero">0</option>
    <option value="One">1</option>
    <option value="Two">2</option>
    
</select>
</form>
</body>
</html>


good luck ;)

__________________________________________________________
"The only difference between me and a mad man is that I'm not mad."
- Dali
 
oops forgot to clean up the script after I got it working, You dont need the variable line, just the alert is fine, and you can alos call this from a link as you were before If you like..

__________________________________________________________
"The only difference between me and a mad man is that I'm not mad."
- Dali
 
Hi Glen and codeone,

Thanks for your input, much appreciated!

This works on IE6 (PC), which is all I need because it is an Intranet project (it also returns the alert value 'NumberOne')

<form name="TestForm" method="post" action="">
<select name="TestSelect">
<option value="select..." selected>Select...</option>
<option value="1">1</option>
<option value="NumberOne">number one</option>

</select>
</form>
<a href="#" onClick="document.TestForm.TestSelect.value = 'NumberOne';alert(document.TestForm.TestSelect.value)">test</a>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top