How can I access the value of imgWidth? This is giving me undefined.
somejQueryMethod(function() {
var imgWidth;
$("<img/>").attr(
"src",
$("#SomeImgSelector").attr("src")
).load(function() {
imgWidth = this.width;
});
// use imgWidth here
// imgWidth is undefined here
});
Background info: From this SO question I assumed that as
imgWidthwas being defined outside of theload()method, it would always be available outside ofload(). Which makes me wonder why defineimageWidththere and not inload()?
undefinederror?$.load()callback runs, which means you need the callback to useimgWidth(basically, where you've set the variable).$.load()is asynchronous, which means it allows the code to continue while it works.