/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] uuugggghhhh… "text" property of an OPTION parameter of a SELECT element

when you select one of the options, this script alerts the “text” and “value” properties of the option that was selected. however, “text” always returns “undefined” using the code below UNLESS i use the absolute DOM value to access it…?!?!?!? what! why??

[code]<script type=”text/javascript”>
function update(obj)
{
alert(obj.text) // undefined
alert(document.stores.city.options[1].text) // works fine

alert(obj.value) // works fine
alert(document.stores.city.options[1].value) // works fine

// since the bottom two work, shouldnt the top two also work!??!
}

</script>

<form name=”stores”>

<select name=”city” onchange=”update(this)”>
<option value=”1″>KMART</option>
<option value=”2″>SEARS</option>
</select>

</form>[/code]

i only tested this in IE

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsSep 29.2007 — [CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body>
<script type="text/javascript">
function update(obj){
alert(obj.text) // undefined because the obj is the select which does not have a text property
alert(obj.options[1].text) // works fine because the option does have a text property

alert(obj.value) // works fine because the select does have a value property(of the selected option)
alert(obj.options[1].value) // works fine because the option does have a value property

// since the bottom two work, shouldnt the top two also work!??!
}

</script>

<form name="stores">

<select name="city" onchange="update(this)">
<option value="1">KMART</option>
<option value="2">SEARS</option>
</select>

</form>
</body>

</html>[/CODE]
Copy linkTweet thisAlerts:
@Angry_Black_ManauthorSep 29.2007 — i gotcha. i need to use obj.selectedIndex and then DOM from there. gotcha. i completely blanked that the select is calling the function.

also, i REALLY respect that you have that phrase "God loves you and will never love you less." ive never thought to do that, or subconciously i was ashamed? but regardless, good show!
×

Success!

Help @Angry_Black_Man spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...