const button = document.createElement('button') button.textContent = 'Click me' document.body.append(button) button.addEventListener('click', function(event) { console.log(this) }) code example
Example 1: events in javascript
//this is an event detector for a mouseclick with Jquery
$('#id').on('click',function(){
yourFunction(args);
});
Example 2: js eventlistener to add and remove stylings
const form = document.querySelector('form');
const fname = document.getElementById('fname');
const lname = document.getElementById('lname');
const para = document.querySelector('p');
form.onsubmit = function(e) {
if (fname.value === '' || lname.value === '') {
e.preventDefault();
para.textContent = 'You need to fill in both names!';
}
}