[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Get the text value of a mshtml HTMLSelectElement

Posted on 2010-09-22
10
Medium Priority
?
2,808 Views
Last Modified: 2013-12-08
I can get the selectedIndex but how do I get the text (not the value) of the selected item?
0
Comment
Question by:GaryRasmussen
  • 5
  • 4
9 Comments
 
LVL 10

Expert Comment

by:himanshut
ID: 33740018
For instance, there’s a HTML select element identified by the ID #title, and after the user has selected one of the options, you want to get the text and value of the selected option.

<select id="title">
  <option value="1">Mr</option>
  <option value="2">Mrs</option>
  <option value="3">Miss</option>
</select>

Now that the user has selected the 2nd option: Mrs. To get the value (in this case, ‘2’):

$("select#title").val();

Which is also the common function to return the value of all other form controls.

To get the text of the selected option (in this case, ‘Mrs’):

$("#title option:selected").text();


Cheers!
0
 
LVL 1

Author Comment

by:GaryRasmussen
ID: 33740030
This is a C$# application using a webbrowser control.  I do not recogonize this syntax.
0
 
LVL 10

Expert Comment

by:himanshut
ID: 33740053
This syntax is for jQuery. Extended version of Javascript you can say
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:himanshut
ID: 33740060
protected void Button_Click (Object sender, EventArgs e)
{
Label1.Text = "You selected:";

for (int i=0; i<=Select1.Items.Count - 1; i++)
{
if (Select1.Items[i].Selected)
Label1.Text += "
   - " + Select1.Items[i].Text;
}
}
0
 
LVL 1

Author Comment

by:GaryRasmussen
ID: 33740061
I would prefer to just the mshtml objects.  Something like this is getting me close ...

            mshtml.HTMLSelectElement list = (mshtml.HTMLSelectElement)htmlDoc.all.item("City", 0);
            mshtml.HTMLElementCollection options = (mshtml.HTMLElementCollection)list.options;
            Debug.WriteLine("City: " + options.item(null, list.selectedIndex));
0
 
LVL 1

Author Comment

by:GaryRasmussen
ID: 33740076
Thanks but that is javascript.

The mshtml.HTMLSelectElement does not have an Items collection
0
 
LVL 1

Author Comment

by:GaryRasmussen
ID: 33740110
I figured out how to do it but geez does it seem like overkill.  If anyone knows a better way, go ahead and post but for now I gotta move on with this because I am way behind schedule ...

            mshtml.HTMLSelectElement list = (mshtml.HTMLSelectElement)htmlDoc.all.item("StateDropDown", 0);
            mshtml.HTMLElementCollection options = (mshtml.HTMLElementCollection)list.options;
            mshtml.HTMLOptionElement option = (mshtml.HTMLOptionElement)options.item(list.selectedIndex, null);
            Debug.WriteLine("State: " + option.text);
0
 
LVL 10

Accepted Solution

by:
himanshut earned 2000 total points
ID: 33740208
Well,
jQuery eliminated all this coding as I posted in the earlier replies.
For that you need to add the latest jQuery script in your .aspx and then you can try out various things without lengthy coding and DOM elements.

0
 
LVL 1

Author Comment

by:GaryRasmussen
ID: 33740424
Luckily I don't usually have to code like this.  This was just a special case project that had to be done immediately.  Maybe I will get the chance to look into jquery but not right now.

Thanks,
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When you put your credit card number into a website for an online transaction, surely you know to look for signs of a secure website such as the padlock icon in the web browser or the green address bar.  This is one way to protect yourself from oth…
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

872 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