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 6855197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:43:38+00:00 2026-05-27T01:43:38+00:00

new to jquery here and I’ve come up with an Ajax box to appear

  • 0

new to jquery here and I’ve come up with an Ajax box to appear under a link. It works correctly for links on the left side of the page but when a link is on the right, the box aligns incorrectly with the page. I’m using absolute positioning for the box ATM which is why it aligns as it does.

Is there a way to have it align correctly to the right for links on the left-side and then to the left for links on the right-side of the page. Insights really appreciated! Thanks!

http://jsfiddle.net/5hUbG

  • 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-05-27T01:43:39+00:00Added an answer on May 27, 2026 at 1:43 am

    Here is a rough method of doing it:

    http://jsfiddle.net/PdCHZ/12/

    [edit]: I updated it to use the tooltip’s width from the CSS:
    [edit number 2:] if you add this:

     $('<div/>',{html:"", id:'tooltip'}).css({visibility:"hidden"}).prependTo( 'body' );
    

    at the beginning of $(document).ready(function () { then the oWidth code will automatically get the width from the CSS.

    $(document).ready(function () {
        $("a.link").click(function () {
            var $link = $(this);
            $.ajax({
                type: "POST",
                url: '/echo/html/',
                data: {
                    html:'these would be the returned data form the <strong>AJAX</strong> call..'
                },
                success: function (response) {
                    var pos = $link.position();
    
                    // this just gts the number of pixels from the left of the screen
                    // that the element is.
                    var offset = $link.offset();
    
                    //set oWidth to 300 unless you want to create a hidden tooltip
                    //before this, so that it can read the width from that.
                    var oWidth = $("#tooltip").width(); 
    
                    // modifier gets set to -oWidth if the link is less than
                    // pixels from the right of the screen (this makes it pop up 
                    // to the left of the link instead of to the right)
                    // (if the element is more than 300 from the right, then it gets set to 10, like you had it originally)
                    var modifier = (offset.left>$(document).width()-oWidth)?-oWidth:10;
    
                       $('#tooltip').remove();
    
                       // all i've done here is changed your "10" to "oWidth"
                       // so that it either turns into -300 or +10 depending on
                       // where the link clicked on is on the screen.
                       $('<div/>',{html:response, id:'tooltip'}).css({left:pos.left+modifier+'px', top:pos.top+10+'px'}).prependTo( 'body' );
    
                }
            });
        });
    });
    

    You could replace “300” with something like $("#tooltip").width() so that you wouldn’t have to update it, but you probably get the idea.

    The magic happens here:

    var modifier = (offset.left>$(document).width()-oWidth)?-oWidth:10;
    

    If you’re not familiar, this is just a shorthand if statement equivalent to this:

    if(offset.left>$(document).width()-oWidth){
        var modifier = -oWidth;
    } else {
        var modifier = 10;
    }
    

    Then you just add modifier to the pos.left and you have your positioning.

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

Sidebar

Related Questions

New to jQuery here. I've found several web pages that come close to what
New to Jquery, even newer to Jquery Ajax calls - here is my problem:
New with jQuery. Here's what I have so far - basically, I want jQuery+ajax
New but keen jquery user here, please be gentle :) I am using the
new here and new to jQuery. I've searched for an answer to my question/
im new to jquery and javascript.. Here is the code, this is a sample,
I'm new here and I have a problem with the jQuery's function - animate
I have application that brings response via Ajax and creates 5-20 new jQuery click
I'm new to jQuery and have been following the tutorial here for a Panel
Fairly new to Jquery here.... but one thing I have been told and being

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.