drop down list boxes onchange events

I have two php drop down list boxes with the following names:
"txtNme1"
"initsel1"

The "txtNme1" box has a list of names being populated from a database list
The "initsel1" box has a list of integers 1 to 60 , beind populated from a database list
 
I simply want to add an onchange event to the "txtNme1" such that any selection will set the "initsel1" box to a selection of 1
doctorbillTechAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Samuel LiewConnect With a Mentor Commented:
Now you are confusing us. Are there two select boxes or is there a "text box"??

Besides, in your previous code, there are two name attributes. Remove one of them.

Also, the code I provided in the first post assumes you only have one of "txtNme1" and "initsel1" on the page, as you did not state that there are multiple.

So, what is? Please explain in detail and provide generated code.
0
 
Samuel LiewCommented:

<script type="text/javascript">
window.onload = function() {
    document.getElementsByName("txtNme1")[0].onchange = function() {
        document.getElementsByName("initsel1")[0].selectedIndex = 0;
    }
}
</script>

Open in new window

0
 
doctorbillTechAuthor Commented:
how is this function called from the txtNme1 dropdownlist box ?
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jrm213jrm213Commented:
<select onchange='document.getElementsByName("initsel1")[0].selectedIndex = 0;' name='txtNme1'>
<option></option>
...
<option></option>
</select>
0
 
jrm213jrm213Commented:
ps. if that works for you, please award points to sam2192
0
 
doctorbillTechAuthor Commented:
nothing happens
0
 
jrm213jrm213Commented:
so maybe a dumb question, but in  your initsel box, is the first item an empty spot or 1?
0
 
doctorbillTechAuthor Commented:
empty
0
 
jrm213jrm213Commented:
<select onchange='document.getElementsByName("initsel1")[0].selectedIndex = 1;' name='txtNme1'>
0
 
Samuel LiewCommented:
It will have been more helpful if you had posted the generated HTML code for both select fields.
0
 
doctorbillTechAuthor Commented:
it works but in dreamweaver, instead of seeing the text box as before, I see the following on the page:

<select name="txtNme1" class="input_boxes_orders_add" id="txtNme1" onchange='document.getElementsByName("initsel1")[0].selectedIndex = 1;' name='txtNme1'>
0
 
jrm213jrm213Commented:
well you can remove the last part that says name='txtNme1' as you already have that right after the select. Other than that I am not sure why it would display like that in dreamweaver as I don't use dreamweaver.
0
 
doctorbillTechAuthor Commented:
message to jrm213jrm213:
Correct - there was a duplicate name. It now works fine
Thanks for your help and consideration for Sam

sam2912:
Sorry - in future I will be more precise with my code
Thanks very much
0
 
doctorbillTechAuthor Commented:
Very fast and professional responses
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.