I'm about to enter jQuery world in a more specific way, so i've written a function to center absolute positioned anchor elements within relative positioned divs, by setting css left property according to the child & parents outerWidth.
Here it is:
$.fn.moreCenter = function() {
var sblockWidth = $(this).outerWidth();
var moreWidth = $(this).children('a').outerWidth();
var moreLeft = (sblockWidth - moreWidth)/2;
$(this).children('a').css('left', moreLeft + 'px');
};
then I target the parent div by their class:
$(document).ready(function() {
$('.single-block').moreCenter();
});
The point is that .single-block structure is repeated in many parts of the page, and each .single-block may be of different widths. This is the reason why the function generalizes the parent div using (this).
However, the function seems to apply the same left value to each ".single-block a" element, calculated on the basis of the first .single-block it finds, without re-calculating it for each element.
What's wrong with the code?