0

I have this jQuery code which centralizes an image within a DIV horizontally and vertically:

var $itemImage = $('.a-img');
$itemImage.each(function() {
    var $img = $(this);
    var h = $img.height();
    var w = $img.width();
    $img.css('margin-top', +h / -2 + "px").css('margin-left',+ w/ -2 + "px");
});

It works when the page is first loaded. However, there's a link on the page that will show a pop-up box where you can update the photo. Once you click "Upload" on the pop-up box, the image on the main page will change without refreshing the entire page. The jQuery code that initially applied to the image no longer works (the image is no longer centralized).

How do I solve this problem?

1
  • This looks like a problem you should be using CSS for and then it would automatically work for new images. Commented Oct 10, 2012 at 4:44

1 Answer 1

1

Put this into a function, and call this function when you load the image. Oh, and this is JavaScript. Remove the $ on the variables: itemImage instead of $itemImage, img instead of $img.

Sign up to request clarification or add additional context in comments.

2 Comments

Prefixing variables in javascript with $ is a fairly common way to indicate they are a reference to a jQuery object. You will notice his h and w variables do not have dollar signs in front of them.
I followed this and I used window.opener.myFunction(); on the child window. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.