0

This script doesn't work.. Where is the error? With .onload it's OK

$(document).ready(function() {

var img = new Image();
img.onClick = function() {
$("div").animate({width:this.width, height:this.height});
}
img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';
});

thanks for the aid.

5
  • this has nothing to do with Java Commented Dec 8, 2012 at 18:53
  • What exactly "does not work"? What has to happen that you can say that it works? There is no JS error at least... Commented Dec 8, 2012 at 19:04
  • @migg jsfiddle.net/gphp/CHuBx/5 Commented Dec 8, 2012 at 19:12
  • 1
    @Giovanni So the script does something, but please explain what you are trying to achieve and what is not working. Until then it is only guesswork. A script without requirements always works as long as it does not produce runtime errors. Commented Dec 8, 2012 at 19:20
  • sorry for bad english.. this is my situation jsfiddle.net/gphp/CHuBx/6 i have a div with 100px height and width with a image background. when i click on div i would like the div change dimension with the image dimension Commented Dec 8, 2012 at 19:26

3 Answers 3

3

Its .onclick not .onClick, javascript is case sensitive.

Sign up to request clarification or add additional context in comments.

4 Comments

@Giovanni How do you know it doesn't work if you don't add the image to the document? jsfiddle.net/mowglisanu/CHuBx/1
i don't have the image in the document, only url. with onload it's work jsfiddle.net/gphp/CHuBx/3
How can you click the image if it's not in the document?
@Giovanni you have to attach the click handler to the div to accomplish what you want jsfiddle.net/mowglisanu/CHuBx/7
1

Since you're using jQuery anyway, why not just stick with it:

$(function(){  // this is a shortcut for $(document).ready
   var img = $('<img src="http://www.google.com/intl/en_ALL/images/logo.gif>');
   img.on('click', function(){
     $('div').animate(...)
   })
   img.appendTo('body')
})

Comments

0

Try this:

var img = new Image();
img.src = 'http://www.google.com/intl/en_ALL/images/logo.gif';

$(img).live("click", function() {
    $("div").animate({
        width: $(this).width(),
        height: $(this).height()
    }, 1000);
}).appendTo('body');​

Comments