Grab a css property with jquery

Asked

Viewed 1,617 times

1

Could I check a css property with pure jquery or js?

Note: Using is.(':Visible') is not feasible in what I need.

Example:

var display = $('.test').get('display').val()
        if(display == 'block')
        {
            alert('block');
        }

2 answers

2

var display = document.querySelector('.sua_classe').style.display;
if(display == 'block')
{
    alert('block');
}

2


Yes, you can use css to read the computed properties of an object, example: $('#id').css('background-color');

Reference: http://api.jquery.com/css/

console.log($('#div1').css('display'));
console.log($('#div2').css('display'));
console.log($('.divClass').css('border'));
#div1 {
  display: inline;
  background-color: red;
  width: 50px;
  height: 50px
}

#div2 {
  display: inline-block;
  background-color: blue;
  width: 50px;
  height: 50px;
}

.divClass {
   border: solid 1px #000
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='div1' class='divClass'>
<div id='div2' class='divClass'>

  • 1

    It worked perfectly for what I need, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.