Link to home
Start Free TrialLog in
Avatar of Marc Davis
Marc DavisFlag for United States of America

asked on

ASP.NET DropDown list item text from JavaScript?

I have a ASP.NET DrownDown and I need get the selectedItem.Text from JavaScript.

How can I achieve that? Any examples would be great so I have a clear/concise understanding.

Any assistence would be greatly appreciated.
Avatar of rajapandian_81
rajapandian_81
Flag of India image

Try below code
var text = document.getElementById('<%=DropDownList1.ClientID%>').options[document.getElementById(vddlSystem).selectedIndex].text;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of rajapandian_81
rajapandian_81
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marc Davis

ASKER

Worked like a charm and thank you very, very much. I didn't realize one could use the getElementByID on the document as well.

I also found that this works as well:

var oDDL = document.all("ctl00_cphBody_DropDownList");
var curText = oDDL.options[oDDL.selectedIndex].text;

As you can see by this there is a Masterpage.

But you're solution worked out in a excellent fashion as well.

Thanks and much appreciated.
Glad to help  :)

If master page is used we can use ClientID to get the control.
On the other variation that I found that worked as well?

Because I tried this:

var oDDL = document.all(document.getElementById('<%=DropDownList1.ClientID%>');
var curText = oDDL.options[oDDL.selectedIndex].text;

But that did not work.

To get id of control we have to use '<%=DropDownList1.ClientID%>'
document.getElementById('<%=DropDownList1.ClientID%>') will return object.

Below one will work.

var oDDL = document.all('<%=DropDownList1.ClientID%>');
var curText = oDDL.options[oDDL.selectedIndex].text;
You're right, that would have brought back the object. Don't know what I was thinking except for many things at a time. :-)

Yep, that worked. I thought I did that before and it didn't work.

Not too sure what I did but it worked and thanks again.