TWebBrowser how set caret position to end of INPUT(text) field on IE8
-
14-11-2019 - |
Question
I have an application that loads a webpage via TWebBrowser and on this page I have some HTML inputs. What I want is to change the value of an input and set the caret position to the end.
This is what I have at the moment:
procedure SetInputValue(Document : IHTMLDocument2; const ElementId, NewValue : String);
var Doc : IHTMLDocument3;
El : IHTMLElement;
begin
Doc := Document as IHTMLDocument3;
if Assigned(Doc) then
begin
El := Doc.getElementById(ElementId);
if Assigned(El) then
begin
if El.tagName = 'INPUT' then
(El as IHTMLInputElement).Value := NewValue;
(El as IHTMLInputElement).select;
end;
end;
end;
This piece of code sets the input value and highlights the text portion. I am aware of the IHTMLInputTextElement2 Interface but it is only available from IE9
Solution
You should use IHTMLTxtRange
var Tr: IHTMLTxtRange;
Tr := (El as IHTMLInputElement).createTextRange;
Tr.collapse(true);
Tr.moveEnd('character', Length(NewValue));
Tr.moveStart('character', Length(NewValue));
Tr.select();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow