Dmitry
Dmitry

Reputation: 14622

How to set position of element equals to position of another element?

The simple question. How to set position of element equals to position of another element by jquery or javascript?

The following code doesn't work:

$('#credit_tip').css('top', $('.credit').position().top);
$('#credit_tip').css('left', $('.credit').position().left);

Upvotes: 1

Views: 2295

Answers (2)

Dmitry
Dmitry

Reputation: 14622

This helps me:

$('#credit_tip').css('top', $('.credit').offset().top);
$('#credit_tip').css('left', $('.credit').offset().left);

Upvotes: 1

Jai
Jai

Reputation: 74738

See left, top, bottom, right these positons will only work if your specific element is relatively or absolutely positioned.

If your code is not affecting that way, then there is more possibilities that your div with id of credit_tip does not has a position of relative or absolute.

So what you can do is position the div to relative or absolute in css:

#credit_tip{
     position:relative;
 }

or with jquery:

$('#credit_tip').css({'top' : $('.credit').position().top, 
                      'left' : $('.credit').position().left,
                      'position':'relative'});

See the fiddle here

and there might be possiblities that you are not calling jquery library in the document or might be some path error. So for this you can try CDN Hosted jQuery library.

link to cdn host

Upvotes: 0

Related Questions