Gregor Gregor - 4 months ago 10
jQuery Question

jQuery selector as parameter is not working

I have this little function to print the image size.
while it works when I just call the function
with

size('#prototype);
for example, it is not working anymore on
resize('#protoimg)
.

Why? Can anyone help me out, please?

$(document).ready(function(){
function size(a){
var height = $(a).height();
var width = $(a).width();
$('#picbox').find('p.size').remove();
$('#picbox').append('<p class="size">' + width + '-' + height + '</p>');
}

size('#protoimg');
$(window).resize(size('#protoimg'));
)};

Answer

Try this:

    $(function() {
      size('#protoimg')
    });

    $(window).resize(function() {
        size('#protoimg');
    });

    function size(a){
        var height = $(a).height(); 
        var width = $(a).width();
        $('#target').html('<p class="size">' + width + '-' + height + '</p>');
    }

If #protoimg is an image file, make sure you set attribute width="100%"to avoid overflow.