Passing a function into an EJS template for use in an onclick event?

為{幸葍}努か 提交于 2019-12-08 16:12:29
Radian Jheng
  • You should put your js function in the .ejs file instead of the server file.
  • You should pass the function name instead of the entire function code.

app.js

var html = template.render({ clickHandler:"func1();" })

EJS

<p onclick='<%= clickHandler %>'>Click Me</p>

<script>
  function func1(){
    console.log("I am in the click handler 1!"); 
  }
  function func2(){
    console.log("I am in the click handler 2!"); 
  }
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!