Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Javascript using onclick?

If I set the following up:

<script type="text/javascript" src="https://test.com/abc"></script> 

Between the head tags, the code runs when the page starts up. Is there any way of stopping this from running during startup and instead running it using the onclick method from a html button?

like image 797
WPZA Avatar asked Oct 28 '25 14:10

WPZA


1 Answers

Take the javascript in your file and wrap it in a function. You can then assign that function to the onclick attribute of a button.

<button onclick="myFunction()">Click Me</button>

And your script

function myFunction() { 
    //your existing code goes here
}

Here's a fiddle.

like image 66
takendarkk Avatar answered Oct 31 '25 03:10

takendarkk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!