91

Is there a way to get the style: display attribute which would have either none or block?

DIV :

<div id="ctl00_MainContentAreaPlaceHolder_cellPhone_input_msg_container" class="Error cellphone" style="display: block;">

     <p class="cellphone" style="display: block;">Text</p>

</div>

I know that there is a way to find out if the DIV is hidden or not but in my case this div is dynamically injected so it always shows up as visible false thus I cannot use that :

$j('.Error .cellphone').is(':hidden')

I am able to get the result "display:block" using :

$j('div.contextualError.ckgcellphone').attr('style')

Is there a way to get just the value "block" or "none" or is there a better/more efficient way to do this?

niton
  • 8,771
  • 21
  • 32
  • 52
Murtaza Mandvi
  • 10,708
  • 23
  • 74
  • 109

5 Answers5

133

You could try:

$j('div.contextualError.ckgcellphone').css('display')
gnarf
  • 105,192
  • 25
  • 127
  • 161
83

If you're using jquery 1.6.2 you only need to code

$('#theid').css('display')

for example:

if($('#theid').css('display') == 'none'){ 
   $('#theid').show('slow'); 
} else { 
   $('#theid').hide('slow'); 
}
raphie
  • 3,285
  • 2
  • 29
  • 26
35

this is the correct answer

$('#theid').css('display') == 'none'

You can also use following line to find if it is display block or none

$('.deal_details').is(':visible')
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Seetpal Singh
  • 359
  • 3
  • 3
  • 2
    Since you're attempting to do an equality check, shouldn't you be using '===' instead of '=='? – user110857 Feb 28 '13 at 14:57
  • 7
    Checking with .css("display") and .is(":visible") are not the same things. If parent element has "display: none", they will give different results. Be careful. – xecute Apr 29 '14 at 22:49
3

My answer

/**
 * Display form to reply comment
 */
function displayReplyForm(commentId) {
    var replyForm = $('#reply-form-' + commentId);
    if (replyForm.css('display') == 'block') { // Current display
        replyForm.css('display', 'none');
    } else { // Hide reply form
        replyForm.css('display', 'block');
    }
}
josliber
  • 43,891
  • 12
  • 98
  • 133
2
//animated show/hide

function showHide(id) {
      var hidden= ("none" == $( "#".concat(id) ).css("display"));
      if(hidden){
          $( "#".concat(id) ).show(1000);
      }else{
          $("#".concat(id) ).hide(1000);
      }
  }
byrop
  • 21
  • 1