Meaningless Drivel is fun!
The moose likes HTML, CSS and JavaScript and the fly likes Why I can apply Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Why I can apply "this" ?" Watch "Why I can apply "this" ?" New topic

Why I can apply "this" ?

Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 465
Good day,

Just trying out some sample for jQuery

Why i can't get the result of source location with "$(this)" when i used ".each" function in jQuery ?
I can only get the result by replace it using

Thanks in advance !
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
The forum markup tags inside of code tags do not work. Look at the code!

jQuery does not have a .src, you would need to use .attr() to get to it.

Also why are you using var elems = document.getElementsByTagName("img"); when you can just do $("img") in jQuery?

Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

$(this) is a jQuery wrapped collection. Check the documentation and you'll see src isn't a property of that object.

If this represents the element, just try this.src or $(this).attr('src') (I haven't tested this, though).
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 465
Awesome, Thanks for explanation Eric and Matthew !

here my sample and it work under this.alt and $(this).attr('src') and $(this).prop("src");

I agree. Here's the link:
subject: Why I can apply "this" ?
It's not a secret anymore!