Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

select list option data 3

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

I've tried googling, I guess i'm using the wrong search terms... anyhow.

I want to make a var = the value between the option tag, not the value attribute of the option tag.

so normally I would use...
Code:
var type = document.getElementById('type').value;
to get the value selected from a select list.

what would I use to get the text content displayed in the drop down for the selected item?

thanks
1DMF

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
lol typical. Knew the answer and didn't know it!

Many thanks.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
well looks like I thanked you to soon and a premature star.

I can't seem to get it to work, if I alert it i get 'undefined'.

Code:
var comp = document.getElementById('company').text;
alert(comp);

any ideas?


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Hi

Interesting. With this code I get "this is what you want" :
JavaScript:
function huh()
{
  var [blue]comp[/blue] = document.getElementById('[green]company[/green]').text;
  alert([blue]comp[/blue]);
}
HTML:
<form action="">
<select>
<option value="this is not what you want" id="[green]company[/green]">[red]this is what you want[/red]</option>
</select>
</form>
How your code looks like ?

Feherke.
 
ok worked it out, you cannot use it in the way you have shown, you must use the options[] with the selectedIndex to gain access to the text...
Code:
 var comp = document.getElementById('company').options[document.getElementById('company').selectedIndex].text;
:)


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
dunno, try adding a few more options to the list and choosing one, then alert, see if it works then?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Hi

Oops. So the element with company [tt]id[/tt] is the [tt]select[/tt] ? Then I misunderstood your question. After you mentioned "option tag" twice, I thought that is the one which has the [tt]id[/tt]. Like in my example.

Then yes, you have to use the good old [tt]options[/tt] array to pick the selected item. Only after that you can get any property of the [tt]option[/tt] element.

Feherke.
 
lol - amazing how a simpe thing can drive you crazy when miss-interpreted, or miss-represented.

At least you gave me enough info to work it out for myself :)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top