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

Can you change the selected row of a select box? 2

Status
Not open for further replies.

LyndonOHRC

Programmer
Joined
Sep 8, 2005
Messages
603
Location
US
I have a select dropdown where the first (and default selected) element is always blank.

If a user checks a checkbox I have on the form can the select be set back to that top element?

Lyndon

Code:
<form name="myform" me
<form name="myform" method="post" action="nextpage.cfm">
 Tax Free: 
 <input type="Checkbox" name="NoTax" onclick="if (this.checked) SetDropDownToZero()">
<br>
 Tax Rate:
 <select name="TaxRate">
  <option value="0" selected>&nbsp;</option>
  <option value="8.5">Oklahoma City</option>
  <option value="8.87">Norman</option>
 </select>
</form>

<script type="text/javascript">
 function SetDropDownToZero()
 {
  //I have no idea
 }

</script>
 
Code:
 function SetDropDownToZero()
 {
  [red]document.forms['myform'].elements['TaxRate'].selectedIndex = 0;[/red]
 }

I think that ought to do it.

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
You can set the value of a textbox both by using the .selectedIndex value (as dave showed above) or by directly setting the value of the textbox. Since we know the value of the first element of your dropdown is 0 we can set the value of the dropdown to 0 to make it jump to that option.

Code:
 function SetDropDownToZero()
 {
  document.forms['myform'].elements['TaxRate'][!].value[/!] = 0;
 }

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Thanks, both solutions work great.
 
I have additional information on this thread. I wanted to share it for anyone using this solution in the future.

While both solutions gave me the desired behavior while using my data entry page; each method rolled the select dropdown back to my top choice (element 0).

I ran into a problem, however, when writing my action page that demonstrated (to me) exactly how each method works.

When using solution 2 the value of Form.TaxRate was numeric zero and not the text value I had set it to in my html code.

I ran a string compare operation on it in my action page and, of course, got an error.

When using solution 1 the value of Form.TaxRate, in my action page, is the same as I had set it in my data entry page.

Thanks, hope this helps another newbie like me. :~)
Lyndon

Solution 1:
Code:
document.forms['myform'].elements['TaxRate'].selectedIndex = 0;

Solution 2:
Code:
document.forms['myform'].elements['TaxRate'].value = 0;


 
Good of you to post a follow-up like that!

Dave

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...east is east and west is west and if you take cranberries and stew them like applesauce
they taste much more like prunes than rhubarb does
[infinity]
 
No problem.

About all I have to offer are the results of my errors. [ponder] But I'm more than happy to share.

Lyndon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top