-4

I have a big JavaScript Code in a html file like <script>CODE</script>

Now, this whole paragraph should only execute if the user clicks on a button (or something like that)

for example <button id="MyButton" onclick= ???>MyButton</Button>

Thanks Guys :)

Krishna Prashatt
  • 631
  • 9
  • 18
Rouven H
  • 71
  • 1
  • 8

1 Answers1

2

Wrap your code in a function, and specify that function in your button onclick.

<script>
function doSomething() {
CODE
}
</script>
<button id="MyButton" onclick="doSomething()">MyButton</button>
spist_opp
  • 70
  • 1
  • 7