I can create a button element and add an onclick event, as shown below.
elem.onclick=function() { alert("qweqweqwe"); }
How can I use a predefined function instead of defining a function inside an event? Sort of:
elem.onclick=func();
add an event element to an element that has a specific function to call:
elem.addEventListener("click", func, false); //where func is your function name
The best way that I have learned for myself is as shown below.
const li = document.createElement('li'); li.innerText = "client"; li.id ="key"; li.className = "client-class"; li.setAttribute("onclick","valid;");
1)
function myfunction(){ alert("hello"); } elem.onclick=myfunction();
2)
var myfunction=function(){ alert("hello"); } elem.onclick=myfunction; //or elem.onclick=myfunction.call();
var clickHandler = function(){ }; elem.onclick = clickHandler;
function clickHandler(){ }; elem.onclick = clickHandler;
functions javascript
functions
javascript
, 1, .
, 2, , clickHandler function , .
clickHandler
function
elem.onclick = function(){};
onclick
. , , , . , w3c . , , .
<div id="test">Click me to alert</div>
-
document.getElementById("test").onclick=callme; function callme() { alert("You have clicked me!"); }
HTML:
<button>Button</button> <div id="buttons"> </div>
JS ( jQuery):
$('button').click(function(){ var dyn_button = '<button onclick="javascript:test()">Dynamic button</button>' $('div#buttons').append(dyn_button + "<br>"); }); function test() { alert(123); }
http://jsfiddle.net/IceManSpy/FaXgr/
Source: https://habr.com/ru/post/1533121/More articles:Trying to make a simulation in R - rSet the dynamic Apple menu name for Java program in NetBeans 7.4 - javaМодели как Scala классы классов, взаимодействующие с DAO? - designCan I compile OpenCL code for all current video cards? - compiler-constructionHttpsURLConnection setDoOutput (true) not working - androidDoes a Javascript object (this) use polygons? - javascriptHow to clear value from magic - javascriptНеобходимый инструмент для размытия холста - javascriptJava ThreadPoolExecutor and BlockingQueue to use - javaPySerial: how to understand that a timeout occurred while reading from a serial port? - pythonAll Articles