JavaScript onClick Event Listener
document.getElementById("myBtn").addEventListener("click", function() {
alert("Hello World!");
});
Batman
document.getElementById("myBtn").addEventListener("click", function() {
alert("Hello World!");
});
const element = document.querySelector(".class__name");
element.addEventListener("click", () => {
console.log("clicked element");
});
var someEventHander=function(){
console.log(event,param1,param2);
}
//add listener
document.getElementById("someid").addEventListener('click',someEventHander.bind(event,param1,param2));
function first(){
alert( " hi it's Toofi");
}
task3Element.addEventListener('click', first);
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
message: ""
};
this.handleEnter = this.handleEnter.bind(this);
this.handleKeyPress = this.handleKeyPress.bind(this);
}
// change code below this line
componentDidMount() {
document.addEventListener("keydown", this.handleKeyPress);
}
componentWillUnmount() {
document.removeEventListener("keydown", this.handleKeyPress);
}
// change code above this line
handleEnter() {
this.setState({
message: this.state.message + "You pressed the enter key! "
});
}
handleKeyPress(event) {
if (event.keyCode === 13) {
this.handleEnter();
}
}
render() {
return (
<div>
<h1>{this.state.message}</h1>
</div>
);
}
}
addEventListener(type, listener);
addEventListener(type, listener, options);
addEventListener(type, listener, useCapture);