Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js onclick function call

<!DOCTYPE html>
<html>
<body>

<h1>HTML DOM Events</h1>
<h2>The onclick Event</h2>

<p>The onclick event triggers a function when an element is clicked on.</p>
<p>Click to trigger a function that will output "Hello World":</p>

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

<p id="demo"></p>

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

</body>
</html>
Comment

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>
Comment

PREVIOUS NEXT
Code Example
Javascript :: reducer in react example 
Javascript :: sort arrays according to first array js 
Javascript :: length of set javascript 
Javascript :: how to convert number to character in javascript 
Javascript :: what is JSON TREE 
Javascript :: window.print filename 
Javascript :: datatables server side 
Javascript :: javascript dump strack trace 
Javascript :: how to remove the top border from table react bootstrap 
Javascript :: how to code print in javascript 
Javascript :: how to display array values in javascript 
Javascript :: deduplicate array javascript 
Javascript :: node js currency format 
Javascript :: dayjs tostring 
Javascript :: array javascript some vs every 
Javascript :: angular hostlistener 
Javascript :: node app listen change ip 
Javascript :: https error response with status 200 angular 
Javascript :: filter multidimensional array javascript 
Javascript :: how to run an existing react project 
Javascript :: javascript string replace all 
Javascript :: loadtest node 
Javascript :: Pass Props to a Component Using defaultProps in react 
Javascript :: javascript trim string 
Javascript :: js if string not empty 
Javascript :: start pm2 node process with flags 
Javascript :: javascript debouncing 
Javascript :: javascript array remove middle 
Javascript :: how to load link in new window using js 
Javascript :: ng model on change 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =