by Sai gowtham

How to check if a element is hidden in JavaScript

Learn, how to check if a (html) element is hidden or not in JavaScript.

In this below example, we are checking if a h1 element is hidden or not by using display:none property.

const h1 = document.querySelector('.myHead');

if(h1.style.display === 'none'){
    console.log('im hidden');
}else{
    console.log('im visible');
}

Similarly, we can check if a element is hidden by using visibility:hidden property.

const h1 = document.querySelector('.myHead');

if(h1.style.visibility === 'hidden'){
    console.log('im hidden');
}else{
    console.log('im visible');
}

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
130,921 students enrolled
48.5 hours of video content
View Course