1

I can't seem to figure this out. I am getting an undefined instead of philadelphia output on this script.

<center>
<form NAME="logform">
<select SIZE="1" NAME="store">
<option name='Philadelphia' id ='Joe Smith' value='[email protected]'>Philadelphia</option>
</select>
<br><br>
<input LANGUAGE="JavaScript" TYPE="button" VALUE="Send email"
ONCLICK="location.href = &quot;mailto:&quot; +
document.logform.store.options
[document.logform.store.selectedIndex].
value + &quot;?subject=I would like to buy the &quot; +
document.logform.store.options
[document.logform.store.selectedIndex].
name + &quot; location &quot;"
NAME="Send email">
</form>
</center>

Thanks for the help.

3
  • 2
    never put JS inside HTML :( Commented Feb 9, 2015 at 12:44
  • Use getElementById() Commented Feb 9, 2015 at 12:44
  • I'm still new to JS sorry but any help is appreciated I am able to call the value but not the name. Commented Feb 9, 2015 at 12:47

1 Answer 1

1

Instead of:

document.logform.store.options[document.logform.store.selectedIndex].name

Use:

document.logform.store.options[document.logform.store.selectedIndex].text

Notice that just .name at the end was replaced by .text.

Working Code Snippet:

<center>
<form NAME="logform">
<select SIZE="1" NAME="store">
<option name='Philadelphia' id ='Joe Smith' value='[email protected]'>Philadelphia</option>
</select>
<br><br>
<input LANGUAGE="JavaScript" TYPE="button" VALUE="Send email"
ONCLICK="location.href = &quot;mailto:&quot; +
document.logform.store.options
[document.logform.store.selectedIndex].
value + &quot;?subject=I would like to buy the &quot; +
document.logform.store.options[document.logform.store.selectedIndex].text + &quot; location &quot;"
NAME="Send email">
</form>
</center>

Source

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I will be accepting your answer as soon as it lets me. May I ask why it needs to be .text instead of .name?
@hyperj123 If you check the MDN documentation for <option>, name is not a valid attribute of <option>. So, to get the option text, they have provided .text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.