The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

> How extract text from form element using <option>, Javascript extracts the assigned value, how do I get the text?
Ann-Marie Foster
post Aug 5 2018, 12:48 AM
Post #1





Group: Members
Posts: 7
Joined: 5-August 18
Member No.: 26,686



Here is the COUNTRY input of a checkout form.

CODE
  HTML:-

Country <select  NAME="Bill Country" ID="BillCountry" ONCHANGE="UpdateCheckout()">
<option value="US">USA</option>
<option value="AF">Afghanistan</option>
<option value="AL">Albania</option>
<option value="DZ">Algeria</option>
<option value="AD">Andorra</option>
. . . . . .
</select>        


The above displays a form element where the customer can select their country.
java script: document.getElementById("BillCountry").value;
Gets the 2 character "Country Code" needed by PayPal.
But I would like to also access the TEXT of the particular country selected for printing a summary.
After they select a country the full text shows on the form element but HOW DO I EXTRACT THE TEXT?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
 
Reply to this topicStart new topic
Replies
CharlesEF
post Aug 8 2018, 05:35 PM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,981
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



Funny, when I do a bing search (I don't use google for anything) for 'javascript get selected option text' all the main results shows examples like my code sample. In fact, pandy's code sample is the only place I have ever seen that syntax used.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Aug 9 2018, 12:49 AM
Post #3


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,731
Joined: 9-August 06
Member No.: 6



You would have seen it twice had you follow my link to the MDN reference. biggrin.gif

I have found examples of your way of doing it now, but only at forums and such.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Posts in this topic
Ann-Marie Foster   How extract text from form element using <option>   Aug 5 2018, 12:48 AM
pandy   You need to use the 'text' property instea...   Aug 5 2018, 01:45 AM
pandy   Now I remember what I concluded last time I broode...   Aug 5 2018, 01:53 AM
CharlesEF   You need to use the 'text' property inste...   Aug 5 2018, 06:26 PM
pandy   You mean we could just do 'document.getElemen...   Aug 5 2018, 10:04 PM
CharlesEF   You mean we could just do 'document.getEleme...   Aug 5 2018, 10:15 PM
pandy   Oh, sorry, I misread, thought you quoted my code. ...   Aug 5 2018, 11:06 PM
CharlesEF   I didn't expect your code to work. And it has...   Aug 6 2018, 01:50 PM
pandy   But the options are a collection and a collection ...   Aug 6 2018, 09:26 PM
CharlesEF   I'll just keep using my syntax. No need to co...   Aug 7 2018, 12:08 PM
pandy   Same for me. That's the way I learnt it. The l...   Aug 8 2018, 12:33 AM
Christian J   What surprises me is that I can't find an exa...   Aug 8 2018, 06:08 AM
pandy   Of course options can be used. :P It was optionss...   Aug 8 2018, 07:08 AM
Christian J   It was options[] paired with selectedIndex as in ...   Aug 8 2018, 01:18 PM
pandy   [quote name='pandy' post='136043' date='Aug 8 201...   Aug 8 2018, 01:41 PM
Christian J   doesn't that make the construct seem unnecess...   Aug 8 2018, 04:38 PM
CharlesEF   This code is taken from a stackoverflow post.funct...   Aug 8 2018, 01:39 AM
CharlesEF   Funny, when I do a bing search (I don't use go...   Aug 8 2018, 05:35 PM
pandy   You would have seen it twice had you follow my lin...   Aug 9 2018, 12:49 AM


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 26th April 2024 - 09:56 AM