JavaScript   

Pure JavaScript click event listener

This is code snippet is for adding JavaScript click events on multiple elements with the same class name.

HTML Code 

<button class="add-product">Add</button>
<button class="add-product">Add</button>
<button class="add-product">Add</button>
<button class="add-product">Add</button>

JS Code 

let productAddBtns=document.getElementsByClassName("add-product");

for (var i = 0; i < productAddBtns.length; i++) {
    productAddBtns[i].addEventListener('click', addProduct, false);
}

function addProduct(){
  console.log("add product Clicked");
}

This is code snippet is for adding a JavaScript click event on the single element ID.

HTML Code 

<button id="show-product">Add</button>

JS Code

let productShowBtn=document.getElementById("show-product");

productShowBtn.onclick = function(){
showProduct();
};

function showProduct(){
  console.log("show product Clicked");
}

 

Need Professional Support for Your Website Problems?

Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.

Hire Us