Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

call button click event in javascript

//Solution #1 use onClick Method in html tag
<button onclick="myFunction()">Click me</button>

// in <script>
function myFunction() {
  document.getElementById("demo").innerHTML = "Hello World";
}

//Solution #2 Add addEventListener
<p id="demo">Click me.</p>
<script>
const p = document.getElementById("demo");
p.addEventListener("click", myFunction);
function myFunction() {
  document.getElementById("demo").innerHTML = "YOU CLICKED ME!";
}
</script>

//Solution #3 use onClick Method in DOM
<p id="demo">Click me.</p>
<script>
const p = document.getElementById("demo");
p.onclick = function() {myFunction()};
function myFunction() {
  document.getElementById("demo").innerHTML = "YOU CLICKED ME!";
}
</script>
 
PREVIOUS NEXT
Tagged: #call #button #click #event #javascript
ADD COMMENT
Topic
Name
9+3 =