0

I have a div class called siteads where i put js-adbanners in I have another div id called adblockmessage

<div class="siteads"><!-- js-code goes here --></div>
<div id="adblockmessage"><!-- adblock message goes here --></div>

What I want to do should be simple; check the height of .siteads and if is 0px display #adblockmessage as a block (or show / hide)

What I did is this:

function blockAdblockUser() {
    if ($('.siteads').height() == 0) {
        $('#adblockmessage').show();
    else 
          $('#adblockmessage').hide();
    }
}

$(document).ready(function(){
    blockAdblockUser();
});

Can any jQuery captain solve this in seconds please? Thanks in advance!

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

3 Answers3

2

Your jQuery if-else has an error. You can see it in console(F12).

function blockAdblockUser() {        
    if ($('.siteads').height() == 0) {
        $('#adblockmessage').show();
    }
        else $('#adblockmessage').hide();        
}

Fiddle

Sukanya1991
  • 778
  • 3
  • 17
0

Easier way of doing this.

Review:
http://jsfiddle.net/uph9wz76/

Checking if an element is :visible will check if it exists, if it has a non-hidden display type, and if it has dimensions.

More info: https://api.jquery.com/visible-selector/

Josh
  • 3,258
  • 2
  • 19
  • 31
0

If the content of your divs are loaded dynamically, then you will need to use window.load event to get its correct height as discussed here: https://stackoverflow.com/a/13071846/1845408

$(window).load(function() {
   blockAdblockUser();
});

//blockAdblockUser fn goes here 
Community
  • 1
  • 1
renakre
  • 8,001
  • 5
  • 46
  • 99