Check if element exist in JavaScript or not

Check if element exist in JavaScript or not

This would prevent unnecessary error on other pages of focus element not found.

var elementExists = document.getElementById("closepopbtn");
   if(elementExists){
         document.getElementById('closepopbtn').onclick = function() {
      document.getElementById('popup').style.display = 'none'
    };
  }

Leave a Reply

Your email address will not be published. Required fields are marked *