473,387 Members | 1,585 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Trying to get the text value of a selected option from a select

Hi I'm trying to get the text from the selected <option> of a <select>

Expand|Select|Wrap|Line Numbers
  1. function loadSelectQuestion()
  2. {
  3.     var dropdownIndex = document.getElementById('selectCategory').value;
  4.     document.getElementById('selectQuestion').innerHTML = dropdownIndex;
  5. }
  6.  
I can get the value of the select but if I try doing .text instead of .value i get an undefined responce.
Jun 4 '09 #1
6 2961
Dormilich
8,658 Expert Mod 8TB
@chanshaw
because there is no such property in the HTML element (resp. in its JavaScript representation)
Jun 4 '09 #2
I'm sorry, I don't understand. So I can't get the text value of the selected option with javascript?
Jun 4 '09 #3
Dormilich
8,658 Expert Mod 8TB
you can get the text value, but you have to do it properly.

assuming you already have the option element, there are several ways
Expand|Select|Wrap|Line Numbers
  1. // the option element is referenced through "opt"
  2.  
  3. // for browsers which support textContent
  4. text = opt.textContent;
  5.  
  6. // DOM level 1 (works everywhere, but not very elegant)
  7. // if there is only text inside the option element 
  8. text = opt.firstChild.nodeValue;
Jun 4 '09 #4
acoder
16,027 Expert Mod 8TB
The .text is a property of the <option> element/object, not the <select> element, so get the selected option to get the text:
Expand|Select|Wrap|Line Numbers
  1. sel.options[sel.selectedIndex].text
where 'sel' is a reference to the select object.
Jun 4 '09 #5
perfect thank you much

Expand|Select|Wrap|Line Numbers
  1. var theCategory = selectCategory.options[selectCategory.selectedIndex].text 
  2.  
Jun 4 '09 #6
acoder
16,027 Expert Mod 8TB
No problemo .
Jun 4 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
4
by: Dan | last post by:
Can anyone offer suggestions on how to do this or if it is possible? I have a form that uses a drop down box and 2 text fields. What I am trying to do is have the value of each text box set by...
12
by: Randell D. | last post by:
Folks, I have a form called "ourTestForm". Its a test form - nothing special - it contains five input tags - they are named one, two, three, four and five. The input tags are of type...
4
by: Leszek | last post by:
Hi I've got a proble because text inputs made by function are outside frameset and below submit button. What should I do to make it work properly? Here is code function zmiana(ile){
2
by: onetitfemme | last post by:
How come this works http://www.geocities.com/tekmonk2005/testSelectJS00.html works fine in Konqueror, but this http://www.geocities.com/tekmonk2005/testSelectJS02.html doesn't in FireFox,...
4
by: devine | last post by:
Hi All, I am VERY new to Javascript. I have been provided with some code, which will enable me to hide/show a text area and change a submit button dependant on a check box. <!DOCTYPE html...
7
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
4
by: coldfusionstudent | last post by:
i wish to show/appear and dissappear text box based on a the drop down item selected. what do i have to add? thanks under Comm_DEV drop down selection.
1
by: angelicdevil | last post by:
i have listbox 1 which displays status , based on selection of status listbox 2 displays usernames. and based on username selected the textbox displays the email id. its working fine till...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.