Why wrapping in the jQuery object

So here's my answer:
$(function() {
  $('img').attr('src', 'http://placepuppy.it/350/150');
})
I'm simply starting with the jQuery object and passing it an anonymous function.
The anonymous function changes the src of the one img on the page to the URL provided. (Remember,$('img') grabs all of the images on the page, so this is a very bad selector. It works in this case because there's only one , but normally you should use a much more specific selector.)
If I hadn't wrapped my .attr() function in the jQuery object, it would run as soon as it's loaded in the of the document, which occurs before the  tag appears on the page. So nothing would happen.
But by wrapping it up in the jQuery object, it runs when the DOM is ready and I get to see a cute puppy instead!

Comments

Popular posts from this blog

পিৎজা এবং বই

How to Download Video from Youtube - The Easiest way