The initial return value of $(document).height may be inaccurate, but is accurate upon recalculation

I am working on implementing a pop-up screen and I would like to darken the background when it opens. Below is the Javascript code:

$(document).on('click', '.item', function(){
        $("#popUp").css("display" , "block");
        var divId = $(this).attr("id");
        $.get('portfolio/portfolio.xml', function(file){
            $(file).find('item').filter(function() {return $(this).attr("id") == divId;}).each(function(){
                var $item = $(this);
                var name = $item.find("name");
                var description = $item.find('description'); 
                $("#popUpName").html(name.text()) ;
                $("#popUpDescription").html(description.text());
                var im; var n=0;
                $item.find('image').each(function() {
                    var i = "<img src='" + $(this).text() + "'></img>";
                    i += "<div>" + $(this).attr("description") + "</div>";
                    $("#popUpImage").append(i);
                });
                setPopUpHeight();
                $(document).scrollTop(0);
            });
        }); 
    });

    function setPopUpHeight() {
            //alert($(document).height());
            $("#popUp").height($(document).height());
            alert($("#popUp").height());
    }

For instance, in one scenario, the height value was 3701 at first and then became 4196 on the second attempt.

Edit: Changed from $(window).height to $(document).height.
Edit: Note that if you uncomment the first alert, the document height will return the correct value on the second try.

Answer ā„–1

Swap out $(document).height() for $(window).height().

Also, I have repositioned the calls to SetPopUpHeight() and scrollTop(), as they do not need to be executed for each 'item'.

$(document).on('click', '.item', function(){
        $("#popUp").css("display" , "block");
        var divId = $(this).attr("id");
        $.get('portfolio/portfolio.xml', function(file){
            $(file).find('item').filter(function() {return $(this).attr("id") == divId;}).each(function(){
                var $item = $(this);
                var name = $item.find("name");
                var description = $item.find('description'); 
                $("#popUpName").html(name.text()) ;
                $("#popUpDescription").html(description.text());
                var im; var n=0;
                $item.find('image').each(function() {
                    var i = "<img src='" + $(this).text() + "'></img>";
                    i += "<div>" + $(this).attr("description") + "</div>";
                    $("#popUpImage").append(i);
                });
            });
            setPopUpHeight();
            $(document).scrollTop(0);
        }); 
    });

    function setPopUpHeight() {
            //alert($(document).height());
            $("#popUp").height($(window).height());
            alert($("#popUp").height());
    }

Answer ā„–2

When you use $(window).height().top;, the display should appear as expected

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

Purge React Query Data By ID

Identify the Issue: I'm facing a challenge with invalidating my GET query to fetch a single user. I have two query keys in my request, fetch-user and id. This poses an issue when updating the user's information using a PATCH request, as the cach ...

What is the process of compiling HTML code?

In controller, I bind the same data like $scope.name = "<div>geetha teko</div>", this name will now be bound to the HTML like {{name}}. When I view it in the browser, it prints as "<div>geetha tek0</div>". Is there a way to only di ...

Vue's span function is yielding the promise object

In my Vue component, I am using the function getOrderCount to fetch the number of orders from a specific URL and display it in one of the table columns. <div v-html="getOrderCount(user.orders_url)"></div> async getOrderCount(link) { ...

Safeguarding intellectual property rights

I have some legally protected data in my database and I've noticed that Google Books has a system in place to prevent copying and printing of content. For example, if you try to print a book from this link, it won't appear: How can I protect my ...

Encountered an error trying to access the length property of an undefined variable while passing props

I need help with my shopping app project. I am trying to create a feature where if the cart is empty, it should display a message saying "shopping cart is empty." However, when I try to implement this, I keep getting a type error that says "Cannot read p ...

Rails 4 not properly executing success function after rendering JSON response

Currently, I am utilizing the jQuery method $.get to retrieve a JSON response from the server. $.get(SITE_EDIT_HOME_URL,{key: key},function(r){ r = JSON.parse(r); console.log(r); }); This is how I handle the request and send back a response: def g ...

Guide to retrieving data from a URL and storing it in a string variable with JavaScript

Attempting to retrieve the JSON data from a specific URL and store it in a variable. The code snippet below successfully loads the JSON into a div element: $("#siteloader").html('<object data="MYURL">'); However, the goal is to extract t ...

Can an event be passed from one Vue.js component to another without relying on a global EventBus?

I have two independent components that are not directly related to each other. However, I want them to be able to communicate so that when an event is triggered in one component, the other component can respond accordingly. For example, let's conside ...

Is there a clash with another code causing issues in troubleshooting a straightforward jQuery function?

jQuery(document).ready(function() { jQuery("#bfCaptchaEntry").on("click", function(){ jQuery("#bfCaptchaEntry").css("background-color", "#FFFFFF"); }); jQuery("#bfCaptchaEntry").on("blur", function(){ jQuery("#b ...

Read a local file using the HTML5 FileReader

I am currently working on developing an offline application that can read text from a locally stored text file. I have been researching and found that using html5 and FileReader can make this possible. My goal is to set a hard-coded relative path for the ...

MongoDB Sorting Techniques

I'm struggling to figure out how to retrieve an ordered list from MongoDB data. I need to fetch the results from a Mongo collection using return and .find(), structured like this: For instance, let's say I have one collection for cars with 10 do ...

A little brain teaser for you: Why is this not functioning properly on Google Chrome?

Have you noticed that the code below works perfectly in Firefox, but fails in Chrome when trying to 'post' data? $("a").click(function() { $.post("ajax/update_count.php", {site: 'http://mysite.com'}); }); Hint: Make sure you are us ...

Executing a component's function from a JavaScript file

Is it possible in React to call a function or method of a component from a separate JS file in order to modify the component's state? Here are three example files: First, App.js import React,{Component} from 'react'; import Login from &ap ...

Get rid of the horizontal scroll bar and expand the width of the table

Normally, Tabulator limits the maximum width of the table and adds a horizontal scroll bar at the bottom. Is there a way to eliminate this scroll bar and make Tabulator expand the width of the table instead (resulting in the horizontal scrollbar appearin ...

Submit the form without displaying any output in the browser, not even in the view source code

I have a basic form that needs to be submitted multiple times, but I want the submission process to be hidden from the browser. Simply using "hidden" or "display:none" won't completely hide the form code when viewing the page source. I tried using PHP ...

Utilizing jQuery to eliminate spaces and prevent special characters: a comprehensive guide

For my website's signup form, I need to enforce certain rules for the username: The username cannot contain any spaces. The username can only include a dot (.) as special character, similar to how Gmail handles usernames in their signup form. I am ...

What is the method for shifting content as the window is resized to ensure it remains in its original position?

My page features a grid with three div elements. Each div is the size of the viewport, resulting in only one div being visible at a time, while the other two remain outside the view. This makes the grid three times larger than the viewport. When resizing ...

Send an array of data from the view to the Controller in CodeIgniter

I have been searching for a solution to this question. However, for some reason, my code is not functioning properly. Here is what I want to achieve: Data Array -> ajax post -> codeigniter controller(save data in DB and redirect to another page) ...

Using Node.js to integrate Stripe payment method

After successfully implementing a stripe payment method with node and express, I encountered some issues. Although the payment runs smoothly and returns a success message, the customer is not being added to the list of stripe customers. Additionally, my no ...

show a function written in JavaScript

Iā€™m currently developing an API and looking to demonstrate a JavaScript code example for invoking this API. While writing a test function in JavaScript, I aim to execute and showcase the code for the JavaScript functions. However, my preference is to ha ...