Computing.Net > Forums > Web Development > Javascript elements option values

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Javascript elements option values

Reply to Message Icon

Name: Spinal
Date: May 17, 2007 at 01:01:56 Pacific
OS: WXP, W2K, W3.1, WBob
CPU/Ram: 1Gb, 384Mb, 2Mb, 2Mb
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: May 17, 2007 at 17:06:20 Pacific
Reply:

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


0
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Javascript elements option values

Font Changing Coding in Javascript www.computing.net/answers/webdevel/font-changing-coding-in-javascript/1778.html

PHP e-mail form: Option element www.computing.net/answers/webdevel/php-email-form-option-element/3147.html

Colored SELECT elements www.computing.net/answers/webdevel/colored-select-elements/1492.html