Welcome to %s forums

<No. 1 Community for developers, by developers/>

Login Register

How to add click event in ES6+

Get support on JavaScript,jQuery, AJAX and other related technology.
Post Reply
admin
Site Admin
Posts: 45

How to add click event in ES6+

Post by admin » Thu Aug 22, 2019 2:46 pm

Code: Select all

const box = document.querySelector('.box');
box.addEventListener('click', () => {
    console.log(this);    
});
HTML:

Code: Select all

<div class="wrap">
    <div class="box">
        <h2>Dummy Title/h2>
        <p class="social">@engg_rky</p>
    </div>
</div>
Now if you ant to add a class dynamically then
ES6:

Code: Select all

const box = document.querySelector('.box');
box.addEventListener('click', () => {
    this.classList.add('newclassname');    
});

Post Reply