Forum > Pas2JS

binding DOM elements in DoRun is not persistent?

(1/1)

nomorelogic:
hi all
I'm playing with pas2js reproducing an example found in blaise pascal magazine.

Development environment is Lazarus 2.2.2 + Pas2Js 2.2.0.
Follow some code from attached project:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TMyApplication.doRun; begin  // Your code here  BindElements;  btnLogin.AddEventListener('click',@DoLoginCLick);   Terminate;end; procedure TMyApplication.BindElements;begin  Writeln('BindElements');  edtEmail:=TJSHTMLInputElement(GetHTMLElement('edtEmail'));  edtPassword:=TJSHTMLInputElement(GetHTMLElement('edtPassword'));  btnLogin:=TJSHTMLButtonElement(GetHTMLElement('btnLogin'));end; procedure TMyApplication.doLoginClick(aEvent: TJSEvent);begin  Writeln('doLoginClick');  // BindElements;   // <- uncomment and all works!  writeln('edtEmail.Value = ', edtEmail.value);    // <- this give the error  DoServerLogin(edtEmail.Value,edtPassword.Value);end; 
At runtime, if I click on LoginButton I get this error:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---project_login.lpr:45 Uncaught TypeError: Cannot read properties of undefined (reading 'value')    at Object.doLoginClick (project_login.lpr:45:40)    at HTMLButtonElement.cb (rtl.js:249:1) 
Anyway if I uncomment "// BindElements" from "procedure TMyApplication.doLoginClick(aEvent: TJSEvent)"  all works well...

Where I'm wrong?

thanks
nomorelogic


nomorelogic:
I got the solution from Michael Van Canneyt
follows the solution


--- Quote ---Normally the bind should be persistent. I use the described technique since several years meanwhile.
Please check if your program code contains an Application.Free after the Application.Run.
If it does, remove it.
Earlier versions of Lazarus generated this statement erroneously when using
the 'new web application' wizard.
--- End quote ---

Navigation

[0] Message Index

Go to full version