Changing the visibility of a div based on the height of another div using jQuery

In my code, I have a division with the class siteads where I insert js-adbanners. I also have another division with the id adblockmessage.

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

The task at hand seems straightforward: check the height of .siteads and if it is 0px, display #adblockmessage as a block (or toggle its visibility).

This is the solution I came up with:

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

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

Any jQuery experts out there who can quickly solve this? Much appreciated in advance!

Answer №1

There seems to be an error in your jQuery if-else statement. Check the console for more details (F12).

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

View example on Fiddle

Answer №2

If you are dynamically loading the content of your divs, utilizing the window.load event is essential to obtain its accurate height. You can find more information about this topic here:

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

//executeCode function implementation

Answer №3

An easier approach to accomplish this task.

For a detailed review, please visit: http://jsfiddle.net/uph9wz76/

By using the :visible selector in jQuery, you can determine if an element is visible by checking its existence, display type (not hidden), and dimensions.

For more information, refer to: https://api.jquery.com/visible-selector/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a proper method for refreshing a DataTable? Encountering issues such as "fnDraw method not found" and "oFeatures property cannot be read."

Upon inheriting the code snippet below: // Display or hide table rows based on checkbox state $("table.data#report-table .warning").toggle($("#warning_checkbox").is(":checked")); $("#warning_checkbox").click(function() { $("table.data#report-tab ...

Efficiently reducing the size of a CSS selector string with javascript

Is there a library that already performs this function? I've only been able to find online tools. The reason I am interested in accomplishing this task using JavaScript is because I need to ensure that the strings a > b, a and a> b,a are conside ...

results without a table

Currently, I am working on a search page in PHP and I want to display the results in a similar style to the image provided. Could you please advise on how I can achieve this type of design using CSS? I would like it to be a fluid design using percentages, ...

CSS animation is supported on most browsers with the exception of Safari

I'm facing an issue with my animation that works fine on all browsers except Safari. I've tried using the autoprefix extension in VSCode to automatically add CSS vendor prefixes, but the animation still isn't functioning correctly. Here&apo ...

Sidebar Masonry - Use Bootstrap for a Unique Layout

My goal is to have the page divided into 4 rows, with Masonry aligning the posts within 3 rows and a sidebar displayed on the right. However, I am facing an issue where Masonry mixes everything up and does not allow me to have a sidebar at the right. HTML ...

Utilizing Multiple Backgrounds in CSS

I am currently working on enhancing the background of my website located at www.softglobal.com.mx. The web page includes two headers, one with the logo and another filling the right top corner. However, I am looking to add a background that will appear b ...

Switch from hover effects to CSS3 animations triggered on page load

I found this code snippet on https://codepen.io/chriscoyier/pen/NWNJpdJ. Can someone help me modify it to change the counter on page load instead of hover? Also, I want the counter to stop at 100 without resetting back to 0. @property --num { syntax: ...

The order of execution is not maintained for $.getJSON() calls within the $.each() loop

As I iterate through an HTML table, I am making a $.getJSON() request based on the data in each row. My goal is to retrieve the data from that $.getJSON call and update the corresponding row with it. Unfortunately, when I run my code, it seems to be execu ...

What is the process for including paths as parameters in jvectormap?

Can you assist me with a simple question regarding jvectormaps? How can I pass paths as parameters using this plugin? $('#worldMap').vectorMap({ map: 'world_mill_en', backgroundColor: "transparent", ...

Pass SASS/SCSS variables to Javascript without the need to export them to CSS files

Context Let's take a look at the _variables.scss file below: /* Setting up color variables */ $white: #fff; $black: #000; $grey: #ccc; // and so on... // Export the color palette for Javascript accessibility :export { white: $white; ...

Replicate the drop file event

Can the drop event be simulated or faked using only JavaScript? How can this type of event be tested? Consider a drag-and-drop upload example like this one on this page. Is it possible to trigger the "drop" event with a file without actually dropping a fi ...

designing a corner tab element

I'm aiming to create a triangular button in the upper-right corner of my website using fixed positioning. It will be an icon on top of a background color with a hover effect. I'm curious if it's possible to achieve this with an angled div or ...

Input information into a JSON container

I've been struggling to find a solution for this issue. Here's the JSON variable I'm working with, which includes the names "rocky" and "jhon": var names = [ "rocky", "jhon" ]; Now, I need to add a new val ...

Troubleshooting: jQuery addClass() function not functioning properly in conjunction with attr("href", something)

I am trying to implement a unique feature for a link using a Bootstrap button, where it only works on the second click. On the first click, the appearance of the link should change slightly. To achieve this, I am utilizing the .addClass(newClass), .removeC ...

Activate jQuery after a vanilla JavaScript function has been executed

I have created a jQuery function that captures any changes made to an input field and stores them in a variable. The input field is connected to a vanilla JavaScript based API library, which I am unable to convert to jQuery. This library is for an address ...

Why is my CSS causing a div with 100% width to be slightly taller than expected?

I am working on a website that I want to adjust to the width of different browsers. Everything seems to be functioning properly, except for the fact that the parent div containing the 100% width divs always appears around 5 pixels too tall. This is causin ...

Creating a loading animation that appears when the submit button is pressed in C#

I am working on a MVC website and I thought it would be cool to have a spinning GIF appear when the submit button is clicked until the next view loads. Here is my current code, but unfortunately it's not working and I can't figure out why. <p ...

Utilize jQuery to encode PHP JSON data

After making an ajax call to PHP, I am receiving JSON encoded data: [ { "id":"4", "name":"Kg", "vital_sign_list_id":"3", "created":"2016-03-01 18:52:27", "modiefied":"2016-03-01 18:52:27" }, { "id":"5", ...

Another option instead of using the .siblings() method would be

Is there an alternative to using the .siblings() method in jQuery for traversing through divs of a specific class in separate container divs? How can this be achieved with the following code: HTML: <div id="container1"> <div id="1"></di ...

Ways to eliminate the white background placed behind the arrow on my bootstrap carousel

I've been attempting to create a video carousel using Bootstrap 5, and one issue I'm encountering is the appearance of a white background when hovering over the arrow. Normally, it shouldn't display a background, but for some reason, it does ...