/    Sign up×
Community /Pin to ProfileBookmark

capture text selected in a select tag

I have a simple select tag on a form

<select name=”req” id=”req’ >
<option value=”1″>James Young</option>
<option value=”2″>Kebu Steward</option>
<option value=”3″>Randy Clarke</option>
</select>

How can I capture the text of the selected item.
example suppose “Kebu Steward” was selected by the user

how could I capture “Kebu Steward” in one value then break it into two
values
var f_name = Kebu
var l_name = Steward

This is my first time with such and issue and I don’t know how to approach the problem.

Thanks

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@zoltankisDec 30.2007 — mylist=document.getElementById("req")
FullName=mylist.options[mylist.selectedIndex].text
fName=FullName.substr(0,FullName.indexOf(" "))
lName=FullName.substr(1+FullName.indexOf(" "))


Note: there is an error in your code:

id="req' is illegal, on the left there is a double qoute, right parenthesis is a single qoute.

You may use any of them, but not both in that way.
Copy linkTweet thisAlerts:
@cedtech31authorDec 30.2007 — thanks worked like a charm
×

Success!

Help @cedtech31 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 5.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

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