简体   繁体   中英

Function passed as parameter not being called

I have a button that when clicked receives a function passed as an argument and the passed function is also an argument to another function created using JavaScript. The challenge I have is that the passed function is not being invoked.

 function alerts(x) { document.getElementById('dialogfoot').innerHTML = '<button onclick="submit(\'' + x + '\')">Yes</button>'; } function submit(x) { x(); } function callthis() { alert('welcome call me'); }
 <button onclick="alerts(callthis)">submit</button> <div id="dialogfoot" style=""></div>

You are passing the string 'callthis' in submit1 rather than the function callthis .

 function alerts(x){ document.getElementById('dialogfoot').innerHTML='<button onclick="submit1('+x+')">Yes</button>'; } function submit1(x){ x(); } function callthis(){ alert('welcome call me'); }
 <button onclick="alerts(callthis)" id='btn'>submit</button> <div id="dialogfoot" style=""></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM