0

my code:

(function($){
    $.fn.extend({
        popupbox: function(options) {

            //Settings list and the default values
            var defaults = {
                photo_url : "",
                photo_id  : ""
            };

            var options = $.extend(defaults, options);

            return this.each(function() {
                var o = options;

                //Assign current element to variable
                var obj = $(this);

                var photo_pic = $('#photo_box_img');

                photo_pic.attr("src", o.photo_url);

                photo_pic.load(function() {

                    var pic_width = photo_pic.width();
                    var pic_height = photo_pic.width();

                    console.log(pic_width);

                }


            });

        }
    });
})(jQuery);

first time i click an image with the above button in the click() function it returns 0 but next time it shows the proper width... why it does that?

stergosz
  • 5,436
  • 13
  • 60
  • 130
  • have you wrapped your script in document.ready() block – Devjosh May 19 '12 at 05:51
  • Where are you binding to the `click` event? Is it in a `$(document).ready` ? – Kyle May 19 '12 at 05:52
  • updated code, its a jquery plugin outside the $(document).ready and i use it when the DOM is ready – stergosz May 19 '12 at 05:56
  • see my updated answer ... are you using the wrong variable? `photo_box` – sirmdawg May 19 '12 at 06:08
  • 1
    jsFiddle: http://jsfiddle.net/Derps/ (Slightly edited due to syntax errors and an uninitialized variable - that is, `photo_box`) – Brilliand May 19 '12 at 06:14
  • 1
    With the jsFiddle I provided, however, I don't get the problem that you're having. – Brilliand May 19 '12 at 06:17
  • `photo_box.width();`... Is it just me or do you not have a variable called `photo_box` ? Did you mean to use `photo_pic` ? Here is a similar question on stack overflow: http://stackoverflow.com/questions/544993/official-way-to-ask-jquery-wait-for-all-images-to-load-before-executing-somethin Hope this helps :) – sirmdawg May 19 '12 at 05:52

0 Answers0