Home DELPHI + TWEbbrowser - append Option(s) and VALUE(s) into an Existing SELECT
Reply: 1

DELPHI + TWEbbrowser - append Option(s) and VALUE(s) into an Existing SELECT

Kristian Sander
1#
Kristian Sander Published in 2017-09-13 08:45:15Z

On a webpage I have an empty select box:

<select id="select" >
</select>

From my Delphi (7 enterprise / XP - sp(3) ) I want to put N OPTION(s) and VALUE(s) into that select.

N is fetched from a server and can be any number between 1 and 150:

something like this:

Only at runtime will I know the precise valyue of N (which might change from run to run - here n = 5):

DELPHI:

For N := 0 to 5 DO
   PutIntoSelect('option' + inttostr(n), 'value' + inttostr(n));

so the result will be:

HTML:

    <select id="select" >
    <option = '0'>value0</option>
    <option = '1'>value1</option>
    <option = '2'>value2</option>
    <option = '3'>value3</option>
    <option = '4'>value4</option>
    <option = '5'>value5</option>
    </select>

How do I do that ?

I have studied IHTMLSELECTelement but I can't find something useful.

Victoria
2#
Victoria Reply to 2017-09-13 10:25:29Z

It's pretty simple. You just get an option element factory and call create to instantiate a new option. The returned option you then add by the add method of your select element.

This code shows how, but it's not safe! You should crack it into pieces, and whenever you ask for reference, check if the asked one is assigned, so take the following as an example of principle:

procedure TForm1.Button1Click(Sender: TObject);
var
  Option: IHTMLOptionElement;
  Select: IHTMLSelectElement;
  Factory: IHTMLOptionElementFactory;
begin
  Select := (WebBrowser1.Document as IHTMLDocument3).getElementById('select') as IHTMLSelectElement;
  Factory := (WebBrowser1.Document as IHTMLDocument2).parentWindow.Option;

  Option := Factory.create('Option 1', 'Value 1', False, True);
  Select.add(IHTMLElement(Option), Unassigned);
  Option := Factory.create('Option 2', 'Value 2', False, False);
  Select.add(IHTMLElement(Option), Unassigned);
end;

HTML you can play with:

<!DOCTYPE html>
<html>
<body>
  <select id="select"/>
</body>
</html>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298751 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO