After weeks of struggle and months of fine-tuning, I have finally perfected this solution. Using JQuery, I have implemented a mechanism to ensure that two elements always maintain the same width.
Check out the JSFiddle
/**
* Fetch basic dimensions of an element
* @author PseudoNinja (email at pseudoninja dot com)
* @returns object
*/
jQuery.fn.getDimensions = function () {
var $this = $(this),
dimensions = {
width: 0,
paddingLeft: 0,
paddingRight: 0,
borderLeft: 0,
borderRight: 0,
getOuterWidth: function () {
return parseInt(this.width) + parseInt(this.paddingLeft) + parseInt(this.paddingRight) + parseInt(this.borderLeft) + parseInt(this.borderRight);
}
};
dimensions.width = $this.width();
dimensions.paddingLeft = $this.css('padding-left').replace(/[^-\d\.]/g, '');
dimensions.paddingRight = $this.css('padding-right').replace(/[^-\d\.]/g, '');
dimensions.borderLeft = $this.css('border-left-width').replace(/[^-\d\.]/g, '');
dimensions.borderRight = $this.css('border-right-width').replace(/[^-\d\.]/g, '');
dimensions.outerWidth = $this.outerWidth();
return dimensions;
};
/** Adjust element width to match target width
* @author PseudoNinja (email at pseudoninja dot com)
* @param {string} target - selector of element with desired width
*/
jQuery.fn.sizeTo = function (target) {
var $this = $(this),
$target = $(target),
myDimensions = $this.getDimensions(),
targetDimensions = $target.getDimensions(),
difference = targetDimensions.outerWidth - myDimensions.outerWidth,
adjOuterWidth = 0
;
if (difference > 0) {
myDimensions.width = myDimensions.width + difference;
adjOuterWidth = myDimensions.getOuterWidth();
$this.width(myDimensions.width);
}
// Maintain size equality
setTimeout(function () {
$this.sizeTo(target);
}, 100);
return $this;
};
// EXAMPLE OF USAGE
// $(function(){
// $("#div1").sizeTo($("#div2"));
// });