Phong Ho
Phong Ho

Reputation: 5

Cannot set offset element for the first click

I want to show my information at the position of pointer when I click on my element (div tag). But in the first click it's not working. It works for the second click.

<div class="active" onclick="showInfo(event)"></div>
<div class="info">This is information<div>

See it in fiddle.

Upvotes: 0

Views: 176

Answers (2)

renakre
renakre

Reputation: 8291

Demo: http://jsfiddle.net/erkaner/2L9ycL2v/2/

$(document).ready(function () {
    $(".info").hide();

    $(".active").click(function (event) {
        $(".info").show();
        $(".info").offset({
            top: event.pageY,
            left: event.pageX
        });
    });
});

Upvotes: 0

itsmejoeeey
itsmejoeeey

Reputation: 310

You cannot set/change the offset of the div before it has been shown.

You need show the div first, and then set the offset. This is how showInfo should look:

showInfo = function(event){
    $(".info").show().offset({top: event.pageY, left: event.pageX});
}

Here is the changed fiddle.

Upvotes: 1

Related Questions