Solved

get text from <select> using javascript

Posted on 2006-11-29
9
1,920 Views
Last Modified: 2008-03-10
<option value="value">description</option>

How I can get the description (not the value) using javascript.

Also, whats difference between id="something" and name="something". php $_POST variables get values on from "Name" or from "id" too ?
0
Comment
Question by:brightwood
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18043189
brightwood,

You can get using innerText on that element.  If the element (i.e. the option tag) has an id attribute then you can easily access it and the text using ...

document.getElementById("option1").innerText

If you need more details or help using it in the page then please provide some html from that page.

PHP $_POST uses the name attribute.  There is a difference between the 2 (name vs id).  They have different uses and just because one is set it doesn't mean the other is assumed.  They don't need to match.  Some doctypes require name and id for the page to be valid and for the server to handle the html.  Let me know if you have additional questions about this.

Let me know if you have any questions or need more information.

b0lsc0tt
0
 

Author Comment

by:brightwood
ID: 18043225
Doesn't seem to work, I get "undefined" instead of the time I'm supposed to get.
0
 

Author Comment

by:brightwood
ID: 18043237
instead of the text* sorry for mistype.
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 18043242
Does the option have an id?  Is it unique (i.e. the value isn't used in another tag/element)?

Depending on your form set up there are other ways to get the option element you want even if it doesn't have a unique id.  Using getElementsByTagName("option") will get a collection of the option tags.  You can loop through these.  If the option's select tag has an id then you can use that to get to the select tag and the use childNode or some other properties to get the option.  Without your html I can't recommend a specific way to do it though.

I hope this helps otherwise please provide the html for at least this select list.

bol
0
 
LVL 9

Expert Comment

by:blue_hunter
ID: 18043465
from your javascript



<script language="javascript">
function jsGrabText(hdl){

alert(hdl.text);

}
</script>


<select onchange="jsGrabText(this)">
<option value="1">First</option>
<option value="2">second</option>
</select>



cheers
0
 
LVL 2

Accepted Solution

by:
Jon_D earned 500 total points
ID: 18043625
Use the following to get the selected option's text, in this case we are using an alert box to show the text :

Assuming we have the following setup :

<form name="InputForm">
<select name="selectmenu" onChange="alert(this.options[this.selectedIndex].text);">
      <option value="1">Option 1</option>
      <option value="2">Option 2</option>
      <option value="3">Option 3</option>
</select>
</form>

The most important part is : this.options[this.selectedIndex].text

If you wanted to get the value from the click of a button you would use this :
onClick="alert(document.InputForm.selectmenu.options[document.InputForm.selectmenu.selectedIndex].text);"

As for your question regarding name & id with PHP, PHP uses the name attribute for POST values. If you have an id but no name attribute, the value will not be posted.

According to www.w3.org, the ID tag serves the following roles in an HTML document :

1. As a style sheet selector.
2. As a target anchor for hypertext links.
3. As a means to reference a particular element from a script.
4. As the name of a declared OBJECT element.
5. For general purpose processing by user agents (e.g. for identifying fields when extracting data from HTML pages into a database, translating HTML documents into other formats, etc.).
0
 
LVL 1

Expert Comment

by:bobbyo
ID: 18044446
I tend to use this method more:

<select id="selectmenu" name="selectmenu">
     <option value="1">Option 1</option>
     <option value="2">Option 2</option>
     <option value="3">Option 3</option>
</select>

<input type="Button" value="Get Value" onclick="get_value();">

function get_value() {
     var obj = document.getElementById('selectmenu');
     alert(obj.options[obj.selectedIndex].text);
}
0
 
LVL 1

Expert Comment

by:bobbyo
ID: 18044452
Sorry, put the function get_value into javascript tags.
0
 
LVL 3

Expert Comment

by:rixlabs
ID: 18046488
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question