Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Howdy! I'm having some issues with Javascript; I have a drop down menu - when the user selects one of the option, two text fields get updated. One contains the drop down menu's "value" field, and the other, the actual text that the user sees.
document.forms['editUser'].elements['username'].value = document.forms['editUser'].elements['Name'].value;
document.forms['editUser'].elements['name'].value = document.forms['editUser'].elements['Name'].options['0'];<select name="Name" onchange='getData()'>
<option selected value ="a">Amelie</option> <option value="b">Betty</option> <option value="c">Cristine</option>
My problem is that last line of javascript:
options['0'] returns: [object HTMLOptionElement]
Which isn't the "Amelie" I was expecting. Idas?

Well, your select list have the name of "Name" and it appears you hav another field called "name" (lower case n). You should use distinct names.
Also, it would be helpful if you provide more of the code - i.e. the full function and the other fields as it would save time from someone having to try and recreate the code necessary to debug it.
Last suggestion, either use id's on your fields and use getElementById() to reference them, or create an object to cut down on the length og the code.
This should fix the problem (I added a form object and field object to make the code easier to read):
function getData() {
formObj = document.forms['editUser'];
selectObj = formObj['Name']
formObj.elements['username'].value = selectObj.value;
formObj.elements['name'].value = selectObj[selectObj.selectedIndex].text;
}Michael J

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |