Quote:
Originally Posted by fyremoon
I tried using getElementsByTagName on the select and then on the option but had no luck with that.
|
I'd guess that people that have trouble with
getElementsByTagName don't realize that it returns a list of elements rather than one element. If you want to match the second
select element in the document, you would use
document.getElementsByTagName("select").item(1). Alternatively, you can access the element list as an array instead of invoking the
item method:
document.getElementsByTagName("select")[1].
To access the first
option element of the second
select element:
document.getElementsByTagName("select")[1].getElementsByTagName("option")[0].
Quote:
Originally Posted by fyremoon
If someone could suggest some way to map the select tag and then extract its option value to being the user selected value, I'd most appreciate it.
|
There's a
select element-specific property called
selectedIndex that you can use to get the index of the selected
option element:
Code:
var second_select_el = document.getElementsByTagName("select")[1];
var selected_option_el = second_select_el.getElementsByTagName("options")[second_select_el.selectedIndex];
There's also a
select element-specific property called
options for accessing the
option element list that's less verbose than using the
getElementsByTagName method:
Code:
var second_select_el = document.getElementsByTagName("select")[1];
var selected_option_el = second_select_el.options[select_el.selectedIndex];
Give your
select elements IDs and you can select them directly via the
getElementById method instead of through a list with the
getElementsByTagName method:
Code:
<select id="my_ID">
Code:
var select_el = document.getElementById("my_ID");
var selected_option_el = select_el.options[select_el.selectedIndex];
You can also access your elements by
name attribute using
getElementsByName, but, like the
getElementsByTagName method, this involves accessing a list of elements:
Code:
<select name="my_name">
Code:
var first_select_el = document.getElementsByName("my_name").item(0);
var selected_option_el = first_select_el.options[first_select_el.selectedIndex];
Quote:
Originally Posted by fyremoon
Can you extract additional information from the id of the option that is selected as I'd like to sum additional information from the user's choice.
|
For this code:
Code:
var select_el = document.getElementById("my_ID");
var selected_option_el = select_el.options[select_el.selectedIndex];
If you want an
option element's
id attribute value,
value attribute value,
selected attribute value, or text content, you would use:
Code:
var id_attr_val = selected_option_el.getAttribute("id");
/* or */ id_attr_val = selected_option_el.id;
var value_attr_val = selected_option_el.getAttribute("value");
var selected_attr_val = selected_option_el.getAttribute("selected");
/* or */ selected_attr_val = selected_option_el.defaultSelected;
var text_content = selected_option_el.textContent;
/* or */ text_content = selected_option_el.firstChild.nodeValue;
/* or */ text_content = selected_option_el.firstChild.data;
/* or */ text_content = selected_option_el.lastChild.nodeValue;
/* or */ text_content = selected_option_el.lastChild.data;
/* or */ text_content = selected_option_el.childNodes[0].nodeValue;
/* or */ text_content = selected_option_el.childNodes[0].data;
/* or */ text_content = selected_option_el.childNodes.item(0).nodeValue;
/* or */ text_content = selected_option_el.childNodes.item(0).data;
To get the option's value, you simply use
selected_option_el.value. This will reflect the
value attribute value unless you didn't specify that attribute. Then it will reflect the text content.
So, for
<option value="green">Green</option>,
selected_option_el.value returns
green, but for
<option>Green</option>,
Green will be returned.