javascript if button clicked code example

Example 1: how to detect a button click in javascript

if(document.getElementById('button').clicked == true)
{
   alert("button was clicked");
}

Example 2: how to add onclick event in javascript

var element = document.getElementById("elem");
element.onclick = function(event) {
  console.log(event);
}

Example 3: how to check if you click something in javascript

// get the element
const element = document.getElementById('profile_title')

// always checking if the element is clicked, if so, do alert('hello')
element.addEventListener("click", () => {
	alert('hello');
});

Example 4: how to check if the button is clicked or not in javascript

if(document.getElementbyId("id do item").clicked == true){
	window.alert("ok")   
}

Tags:

Misc Example