Scope is important!

 
Body: 

After many years of using Javascript, I finally realized the importance of variable scope.
My code consisted of:

function matte_design_change(cid, selected_type, x, y, opening_width, opening_height)
{
//more code...
size = $(el).find("size");
width = size.attr("width") * ppi;
height = size.attr("height") * ppi;
//more code
}

What was happening was that the width and height were global variables and being set to something else somewhere else in the code. All I had to do to fix this issue was add var before these variables to make them local to that function.

function matte_design_change(cid, selected_type, x, y, opening_width, opening_height)
{
//more code...
var size = $(el).find("size");
var width = size.attr("width") * ppi;
var height = size.attr("height") * ppi;
//more code
}

See JavaScript Variable Scope and Hoisting Explained

Tags: