Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2008
    Posts
    117
    Thanks
    14
    Thanked 0 Times in 0 Posts

    jquery: to get image width on page ready

    Hi,
    How can I get the width of an image when the page is ready...?

    I tried the code below, but it always return the value '0'...!

    Code:
    <script type="text/javascript" src="js/jquery/jquery-1.3.1.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
    	var width = $("#image img").width();
    	alert(width);
    });
    </script>
    PHP Code:
    <div id="image">
    <
    img src="xxx.jpg" />
    </
    div
    many thanks,
    Lau

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,188
    Thanks
    10
    Thanked 569 Times in 550 Posts
    ready fires before images load.
    window.onload would work.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #3
    Regular Coder
    Join Date
    Dec 2008
    Posts
    117
    Thanks
    14
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    ready fires before images load.
    window.onload would work.
    thanks, i got the answer now which is i need to put the size of the image in img tag so that javascript can pick up the value on page ready.

    this is the page im working on. if u turn off javascript on your browser then u will see what i want to achieve to degrade the page gracefully when there is no javascript enabled.

    http://lauthiamkok.net/tmp/projects/part/

    thanks,
    Lau


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •