Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8044847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:21:34+00:00 2026-06-05T05:21:34+00:00

So I have this long function in jquery that includes a jquery ajax call,

  • 0

So I have this long function in jquery that includes a jquery ajax call, some css changes, and repositioning the div. Here is my code:

$('.editable').click(function(e) {
    //assign the attributes for the ajax call                         
    $('#employeelist span').html("job: " + $(this).attr("editjob") + "<br />date: " + $(this).attr("editdate").substr(5));
    $('#employeelist').attr("editjob", $(this).attr("editjob"));
    $('#employeelist').attr("editdate", $(this).attr("editdate"));  
    //update the employee list with AJAX
    $.get("getDaySchedule.php", 
        {'job': $('#employeelist').attr("editjob"), 'date': $('#employeelist').attr("editdate")},
        function(responsetext){$('#employeelist ul').html(responsetext);},
        "html"
    );
    //show the employee list
    $('#employeelist').show()
    .css('top',$(this).offset().top+25)
    .css('left',($(this).offset().left+130))
    .css('visibility', "visible")
    .removeClass()
    .addClass($(this).attr('class'));
    //adjust the employee list if it goes outside the viewable area:
    if ($('#employeelist').height() + $('#employeelist').offset().top > $(window).height()) {
        newtop = ($('#employeelist').height() + $('#employeelist').offset().top) - $(window).height();
        newtop = $('#employeelist').offset().top - newtop;
        $('#employeelist').css('top',newtop);
    };
    if ($('#employeelist').width() + $('#employeelist').offset().left > $(window).width()) {
        newleft = $('#employeelist').offset().left - 260;
        $('#employeelist').css('left',newleft);
    };
});

The problem I’m having is that it appears the last section of code (where it repositions the div if it is outside the viewable area) is being run BEFORE the ajax call is done. So basically the height of the div is very short because it hasn’t gotten the response text yet. This results in the div being too tall and going past the viewable area, because it isn’t being repositioned off the correct height. Hopefully that makes sense. Is there something I’m missing in the code? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:21:35+00:00Added an answer on June 5, 2026 at 5:21 am

    Ajax by default is asynchronous, meaning that once it is called the following code will run without waiting for the ajax call to return; therefore, it is not complete when your following code runs. You need to move your code within the success of the ajax.

    $('.editable').click(function(e) {
        var $editable = $(this);
        var $employeeList = $('#employeelist');
    
        //assign the attributes for the ajax call                         
        $('#employeelist span').html("job: " + $editable.attr("editjob") + "<br />date: " + $editable.attr("editdate").substr(5));
        $employeeList.attr("editjob", $editable.attr("editjob"));
        $employeeList.attr("editdate", $editable.attr("editdate"));  
    
        //update the employee list with AJAX
        $.get("getDaySchedule.php", 
            {'job': $employeeList.attr("editjob"), 'date': $employeeList.attr("editdate")},
            function(responsetext){
                $('#employeelist ul').html(responsetext);
    
                //show the employee list
                $employeeList.show()
                    .css('top', $editable.offset().top+25)
                    .css('left', ($editable.offset().left+130))
                    .css('visibility', "visible")
                    .removeClass()
                    .addClass($editable.attr('class'));
    
                //adjust the employee list if it goes outside the viewable area:
                if ($employeeList.height() + $employeeList.offset().top > $(window).height()) {
                    newtop = ($employeeList.height() + $employeeList.offset().top) - $(window).height();
                    newtop = $employeeList.offset().top - newtop;
                    $employeeList.css('top',newtop);
                }
    
                if ($employeeList.width() + $employeeList.offset().left > $(window).width()) {
                    newleft = $employeeList.offset().left - 260;
                    $employeeList.css('left',newleft);
                }
            }
        );    
    });
    

    Edit:

    I have updated the code to fix the issue you were having. Note I also cached the most commonly used jquery selectors. It is always a good idea to cache the selectors that you more than once to improve the performance of your code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this jQuery ajax navigation tabs plugin that I created using some help
On my website I use a long polling jquery ajax function. This ajax call
I have some code in jQuery that iterate through children in div using each().
I have this jquery function: $(window).scroll(function () { if ($(window).scrollTop() > offset.top) { $(#sidebar).css(marginTop,
I have this JQuery function: $(document).ready(function() { $(div.descript_long).each(function() { var descript_length = $(div.descript_long).text().length; if(descript_length
Here's the use case: I have this long form with group of field that
In Android 3.0 (Honeycomb) we have this feature that when we long press any
Long story short, I have a long code that uses jQuery. Lots of files,
I have this long code about a table width some various content. It is
So I have this neat little javascript function that I'm using to print text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.