Solved

Get the text value of a mshtml HTMLSelectElement

Posted on 2010-09-22
10
2,486 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…

863 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

25 Experts available now in Live!

Get 1:1 Help Now