tags:

views:

118

answers:

2

Hi , I have a such code just entering start part for you

$(document).ready(function(){
    //Dynamically size wrapper div based on image dimensions
    $("#tag-wrapper").css({width: $("#show_img").outerWidth(), height: $("#show_img").outerHeight()});

    //Append #tag-target content and #tag-input content
    $("#tag-wrapper").append('<div id="tag-target"></div><div id="tag-input"></div>');

Idea is that the css properties should immediately take place when the page is loaded. But... When I enter first time to the page, it doesnt work, when I refresh the page it works like it should. Any Idea how I can fix that?

+1  A: 

Your code will work if show_img has a width and height set. I believe the reason why it is not working for you is because $(document).ready is called when the DOM is loaded and before the page contents. So at that point in time show_img has not been loaded yet, so it can not get the width and height unless you explicitly set it.

wsanville
I cant make fixed the width and heigh of the image, as the images are very different. There can be a limit on maximums but they are notfixed I will try first answer now
DR.GEWA
Thank you for the hint. I have got with php the image size and set it up dinamically. So now it's working
DR.GEWA
+1  A: 

The images are probably not loaded when you try to take the width and height, so you must bind a function to the load event of the document, which will ensure that all images are loaded before executing:

$(document).load(function() {
    $('#tag-wrapper').css({
        width: $('#show_img').outerWidth(),
        height: $('#show_img').outerHeight()
    }).append('<div id="tag-target"></div><div id="tag-input"></div>');
});
Tatu Ulmanen
This will fail if the image is already loaded by document-ready, which is very likely for a second page load. You could instead bind to `load` on document, which fires when the document *including all images* has loaded.
bobince
@bobince, good point. I've updated my answer.
Tatu Ulmanen
Tatu this code is not working....
DR.GEWA
Anyway thank you Tatu... I have just checked your web-site.. The CSS plugin for icons are great...
DR.GEWA