When utilizing jQuery to retrieve the height of position:absolute elements, the script is being executed twice, resulting in inaccurate values being returned on

The issue I'm encountering with detecting the height of absolutely positioned content is demonstrated in this JSFiddle.

I am working on creating a tabbed panel where users can select a room type and then a sub-type. The sub-types (ul.sub-types) are set to position absolute for proper alignment.

When switching between room sub-types, I need to determine the height of ul.sub-types using jQuery/javascript to adjust the height of the entire panel (div.tab-panel outlined in blue) to match its contents. This adjustment ensures that the page content below the panel is not obscured by the absolutely positioned room subtypes.

Unfortunately, the jQuery function appears to run twice - initially detecting the correct value and adjusting the tab-panel correctly, but then running again and returning an incorrect value.

This behavior is most evident when selecting "Family" on the left and then "Family Double" at the bottom right, as it contains multiple list items to emphasize the height change.

Any insights into what might be causing this issue or suggestions on how to resolve it would be greatly appreciated.

HTML:

<section id="main_col">

<div class="tab-panel">

    <ul id="room-types" class="left-tabs">
        ...
    </ul>
    
</div>

CSS:

/*--- booking tab panels --- */

.tab-panel {
    ...
}

.tab-panel ul.sub-types {
    ...
}
...

jQuery:

$(document).ready(function() {

$('.sub-types li').click(function() {
    // Handle click logic here
});

}); /* end $(document).ready(function() */​

Answer №1

It seems that the code is getting executed twice because hidden elements are also triggering click events. One way to solve this is by using a :visible selector. Additionally, changing the click event to On and binding it to a different DOM element can prevent the click event from being bound only to visible elements at document.ready:

$('.sub-types').on('click', 'li:visible', function() {
    $(this).parent().find('input:radio').attr('checked', false)
    $(this).children('input:radio').attr('checked', true)
    $('.sub-types li').removeClass('selected');
    $(this).addClass('selected');

    var panelHeight = $(this).closest('ul.sub-types').height();
    alert("height: " + panelHeight);
    $(this).closest(".tab-panel").css('height', panelHeight)

});

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

Utilizing Regular Expressions to remove specific CSS attributes from an HTML content

I'm facing a challenge with my telerik RadEditor where users can input HTML that gets saved to my database. While this usually works well, I encounter issues when the CSS attributes position: absolute; or z-index: 100; (any # for z-index) are present ...

Executing a click event on an HTML table dynamically created from an AJAX request

I have a script that creates an html table displaying Users of the application, with options to either EDIT or DEACTIVATE each user. These actions are handled using AJAX. The table structure is as follows: <tr><td><a id='edit_link&apo ...

Can someone assist me in figuring out how to solve selecting multiple radio buttons at once

<script type="text/javascript"> let x = "1.html"; let y = "2.html"; function redirectPage(form){ for(let i=0; i<form.length; i++) { if(form.answerq[i].checked && form.answerw[i].checked && f ...

Prevent animation when expanding the menu in Vue using CSS

The animation only works when collapsing the menu, not when expanding it. I tried adding CSS for setting a max-height, but the animation only functions when collapsing the menu. <template> <div> <p> <button @click="is ...

Is it possible for PHP to return jQuery code and have it execute immediately upon being echoed?

As someone who is new to jQuery and PHP, I have been tasked by my boss to create a login system for a web page that contains sensitive company information. The challenge is that this webpage consists of just one html/php file. Here is what I have done so ...

The .media object in Bootstrap 3 does not experience overflow issues with .dropdown elements

Dropdown menu not displaying fully I have a dropdown menu but it seems to cut off halfway through displaying its content. <div class="media-body"> <h4 class="panel-heading" style="margin:0;"> <div class='btn-toolbar pull-righ ...

Steps to resolve the white-dot border problem plaguing your dropdown menu

On my website, I've designed a drop-down menu with borders set to gray on the top, left, and right sides, while the bottom border is white. This creates a smooth, continuous border that looks great in FF3 and Chrome. However, after upgrading to FF4 o ...

Using jQuery, you can easily apply a new class to a div when it is

I'm currently facing an issue with adding a class of "active" to a div upon clicking it using jQuery. My goal is to apply the css class of active in order to give the button a distinct color (or the hover effect.) Unfortunately, I have been unsuccess ...

Having trouble with PHPmailer resulting in a 504 gateway timeout error

I keep receiving a 504 gateway timeout error from my server when using phpmyadmin to individually send emails to a list of approximately 1200 users. It is crucial for me that the emails are sent out one by one, as I want to avoid exposing any email addres ...

My script is unable to access the session variable

Two $_SESSION variables seem to be inaccessible in any script on my page, yet I can confirm their existence in the PHP code of the same page by using echo to display their values. When trying to display these $_SESSION variables in jQuery using the code b ...

Looking for assistance with Jquery - how to select a newly created DOM element immediately after its creation?

Why is this particular code not functioning correctly on Chrome 4.0 while it works perfectly on FF 3.5? The issue at hand is that the selector $('li#node-'+nodes[i].id +'').append function does not work in chrome, but seems to be functi ...

Failed to send JSON data to WebMethod

I am encountering difficulties while attempting to send JSON to a WebMethod. I have provided the method I am using below. If there is a more efficient approach, please advise me. My goal is to store the JSON object in a database. JavaScript function TEST ...

TinyMCE in collaboration with JustBoil.ME presents an innovative Image Uploader

I am currently facing an issue while using the jbimages plugin with tinymce. Everything seems to be loading fine, but when attempting to upload images, I encounter the following error: Invalid Key Characters Detected. Could someone please explain what th ...

Responsive Alignment of Slanted Edges using CSS

I have been working on creating a responsive diagonal layout with slanted shapes (refer to the image attached). However, I'm facing a challenge with aligning the edges smoothly at different screen sizes, especially when the rows grow and the screen re ...

Potential bug may arise with the hashchange event

Recently, I stumbled upon a rather unique bug in my Javascript code that has left me puzzled. Here's the scenario: within a parent div with relative positioning, there are three child divs positioned absolutely side by side. Each child div is 1200px ...

Place the retrieved data from the API directly into the editor

I've integrated the LineControl Editor into my app and everything is functioning perfectly, except for when I attempt to insert text into the editor. Here's the link to the LineControl GitHub page: https://github.com/suyati/line-control/wiki Fo ...

Can you explain the function of the "Normalize CSS" feature in Fiddle?

I recently came across this interesting fiddle: Demonstration of rounded corners It achieves the desired outcome, however, I noticed that it only works properly when the "Normalised CSS" checkbox is selected. When it's not checked (like in my code), ...

What is the correct placement for $.validator.setDefaults({ onkeyup: false }) in order to deactivate MVC3 onKeyup for the Remote attribute?

After coming across various solutions on how to disable the onKeyup feature of MVC3 Remote Validator, I noticed that many suggest using the following code: $.validator.setDefaults({ onkeyup: false }); However, I'm in a dilemma about where to place t ...

Exciting transition effect for font sizes in CSS

Issue: Hover over the X and wait for the div to collapse completely. Move away from the X Notice how the div jumps to the bottom and then back up. Inquiry: How can I prevent text wrapping while expanding the div during animation? (Currently using del ...

Using Django and jQuery to retrieve a file and display a prompt for downloading the file in the browser

Previously, I had a question about passing files to the browser for download, which was easily achieved by passing strings created at the end of a function to an iframe's src attribute. Now, I have a more complex task at hand – I need to pass pre-e ...