Solved

Get the text value of a mshtml HTMLSelectElement

Posted on 2010-09-22
10
2,507 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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.
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…
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

770 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