Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
How to get Element's Style value assigned from a Class

If I defined a DIV as such

<div id='myDiv' style='display:none;'>inner stuff</div>

I can check it's current display value as such

document.getElementById('myDiv').style.display;

And this returns a string == 'none'.

But, if I set my display style via a class the above statement returns a string of length zero.

<style>
    .myDivStyle {display:none;}
</style>
<div id='myDiv' class='myDivStyle'>inner stuff</div>
<script>
    var x = document.getElementByID('myDiv').style.display == ''; // returns true
</script>

Indeed the DIV is hidden in both cases, but in the latter case, how do I test that it is hidden (e.g. display=none) from javascript code?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can use getComputedStyle to get the styles used on an element and not just on the style property.

var styles = getComputedStyle(document.getElementById('myDiv'));
var display = styles.getPropertyValue('display')
console.log(display);
.myDivStyle {display:none;}
<div id='myDiv' class='myDivStyle'>inner stuff</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Use window.getComputedStyle to get the CSS properties of the element and CSSStyleDeclaration.getPropertyValue to get the value of the display property:

<style>
    .myDivStyle {display:none;}
</style>
<div id='myDiv' class='myDivStyle'>inner stuff</div>
<script>
    var x = window.getComputedStyle(document.getElementById('myDiv')).getPropertyValue("display")
    console.log(x)
</script>

about 4 years ago · Juan Pablo Isaza Relatório

0

I generally use HTMLElement.hidden for handling the visibility of a div. In order to get the status you can just access the property like this:

var x = document.getElementByID('myDiv').hidden

It will return a boolean value, indicating if the element is hidden or not.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda