Solved

Get the text value of a mshtml HTMLSelectElement

Posted on 2010-09-22
10
2,561 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
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…

730 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