brightwood
asked on
get text from <select> using javascript
<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 ?
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 ?
ASKER
Doesn't seem to work, I get "undefined" instead of the time I'm supposed to get.
ASKER
instead of the text* sorry for mistype.
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("opti on") 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
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("opti
I hope this helps otherwise please provide the html for at least this select list.
bol
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
<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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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('s electmenu' );
alert(obj.options[obj.sele ctedIndex] .text);
}
<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('s
alert(obj.options[obj.sele
}
Sorry, put the function get_value into javascript tags.
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("o
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