Quantcast
Channel: User HoldOffHunger - Stack Overflow
Viewing all articles
Browse latest Browse all 55

Answer by HoldOffHunger for How to set onClick with JavaScript?

$
0
0

If you have a named function, i.e., function funcName() {...} already defined, then the process is simply to identify onclick with the name of the function:

document.getElementById("myBtn").onclick = displayDate;function displayDate() {    document.getElementById("demo").innerHTML=Date();}
<button id="myBtn">Try it</button><p id="demo"></p>

This can help keep your code better organized. A function can be quite long, and defining it singularly to a single onclick, and not some independent variable that can be referenced later, might end up creating spaghetti code.


Viewing all articles
Browse latest Browse all 55

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>