Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8675655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:00:21+00:00 2026-06-12T20:00:21+00:00

In my Delphi application I’m using a TWebBrowser control, where I have loaded an

  • 0

In my Delphi application I’m using a TWebBrowser control, where I have loaded an HTML document, containing a <select> element (drop down list) with a few <option> items (drop down list items). Let’s say, I have the following HTML document loaded in my web browser:

<html>
<body>
  <select id="ComboBox">
    <option value="firstvalue">First Value</option>
    <option value="secondvalue">Second Value</option>
    <option value="thirdvalue">Third Value</option>
  </select>  
</body>
</html>

How can I programatically select e.g. the <option>, whose value attribute is thirdvalue ? Or in other words, how can I programatically select the third item in this drop down list, when I know only that this item’s value attribute is thirdvalue ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T20:00:22+00:00Added an answer on June 12, 2026 at 8:00 pm

    You can use the IHTMLSelectElement interface with its selectedIndex property for instance. As a showcase I’ve made the following function.

    SelectOptionByValue function

    The following function tries to find, and select (if found) an <option> (a drop down list item) of a given value attribute value in a specified <select> element (drop down list). If no <option> is found, the current drop down list selection is cleared (no item is selected).

    Parameters:

    • ADocument – the interface to an input HTML document
    • AElementID – ID of a <select> element (element ID of a drop down list)
    • AOptionValue – searched <option> element value (value of a drop down list item)

    Return value:

    If the <option> with a given value is successfully found (and selected), the return value is the index of that option in a specified drop down list, -1 otherwise.

    Source code:

    function SelectOptionByValue(const ADocument: IDispatch; const AElementID,
      AOptionValue: WideString): Integer;
    var
      HTMLDocument: IHTMLDocument3;
      HTMLElement: IHTMLSelectElement;
    
      function IndexOfValue(const AHTMLElement: IHTMLSelectElement;
        const AValue: WideString): Integer;
      var
        I: Integer;
      begin
        Result := -1;
        for I := 0 to AHTMLElement.length - 1 do
          if (AHTMLElement.item(I, I) as IHTMLOptionElement).value = AValue then
          begin
            Result := I;
            Break;
          end;
      end;
    
    begin
      Result := -1;
      if Supports(ADocument, IID_IHTMLDocument3, HTMLDocument) then
      begin
        if Supports(HTMLDocument.getElementById(AElementID), IID_IHTMLSelectElement,
          HTMLElement) then
        begin
          Result := IndexOfValue(HTMLElement, AOptionValue);
          HTMLElement.selectedIndex := Result;
        end;
      end;
    end;
    

    Example usage:

    To select the item with thirdvalue value in drop down list from the HTML document from the question it’s possible to use this code (assuming in the WebBrowser1 component here is loaded that document):

    procedure TForm1.Button1Click(Sender: TObject);
    var
      Index: Integer;
    begin
      Index := SelectOptionByValue(WebBrowser1.Document, 'ComboBox', 'thirdvalue');
    
      if Index <> -1 then
        ShowMessage('Option was found and selected on index: ' + IntToStr(Index))
      else
        ShowMessage('Option was not found or the function failed (probably due to ' +
          'invalid input document)!');
    end;
    

    Example HTML document from the question:

    <html>
    <body>
      <select id="ComboBox">
        <option value="firstvalue">First Value</option>
        <option value="secondvalue">Second Value</option>
        <option value="thirdvalue">Third Value</option>
      </select>  
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Delphi application that has a document browser as the main form.
I have a Delphi application A, which needs to select a certain item in
I have a Delphi application which displays an image using a TImage. The location
I have a Delphi application that communicates with web servers on the Internet using
I have a Delphi application using DevExpress cxGrid (which is connected to database). I
I have finally got my Delphi application to send data using direct sockets with
I have a Delphi application which reads data from an excel spreadsheet using code
I have a Delphi application A, which I need to control from a .NET
I have Windows Forms Application written on Delphi 7 and C++ .dll written using
We have a delphi application which can also run as a sevice . We

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.