Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i get just 1 element JS

i am trying to add hcaptcha to my website but i am doing it in a strange way i want to get if the div with the class ‘check’ is hidden or not but i cannot do it.
here is my code

    var x = document.getElementsByClassName("check")[0]
    var y = window.getComputedStyle(x).display
    alert(y)

it gives me the error ‘cip.html:62 Uncaught TypeError: Failed to execute ‘getComputedStyle’ on ‘Window’: parameter 1 is not of type ‘Element’.
at cip.html:62′

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to verify that x is not undefined in case none of the elements have the class your looking for

var x = document.getElementsByClassName("check")[0]
if (x) {
  var y = window.getComputedStyle(x).display
  alert(y)
} else {
  alert('not found')
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading