Solved

Get the text value of a mshtml HTMLSelectElement

Posted on 2010-09-22
10
2,465 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
10 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 500 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
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.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

705 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now