Page is a not externally linkable
- Code, Content, and Presentation
-- JavaScript and AJAX
---- problems understanding jquery methods in script: get(), and variables


dupalo - 12:33 pm on Jul 18, 2012 (gmt 0)


Hi thanks for that, but I am not sure I understand.
Now, my interpretation of this function is slightly different and I have added some alerts here and there to test it.
function hoverThumbnail(calledFrom){
var $calledFrom = $(calledFrom);
$('.thumbnails a').each(function(){
if($(this).get(0) == $calledFrom.get(0)){
$(this).fadeTo(0,1);
}
else {
$(this).fadeTo(0,0.5);
}
});
}
Basically the hoverThumbnail is called by this function
$(document).ready(function(){
$('.thumbnails')
.mouseleave(function(){
resetThumbnails();
})
.find('a').hover(function(){
hoverThumbnail(this);
});
});

and it is passed a parameter of this, which represents the a tags inside the thumbnails div.
The parameter received by hoverThumbnail - calledFrom - is then assigned to a variable var $calledFrom, although I am not sure about the purpose of this assignment.
ANyway, moving on with this $('.thumbnails a').each(function(){ we effectively say that for each a tag we will run a function.
In here instead if($(this).get(0) == $calledFrom.get(0)){
$(this).fadeTo(0,1);
}
else {
$(this).fadeTo(0,0.5);
}
my interpretation is that we compare the a tag we hovered on ($calledFrom.get(0))) with the a tags in the thumnails div, looping through each of them: let's take a sample run. Say we hover on the second thumbnail then the loop starts and the first comparison is if($(this).get(0) == $calledFrom.get(1): so second thumbnail (index 1) gets compared to the first thumbnail (index 0). If different - and they are because we hovered on the second one - then the opacity goes down to 0.5. When the loop moves to the second element if($(this).get(1) == $calledFrom.get(1) it finds that the 2 a's are the same therefore it leaves the opacity of it to 1. And so on till it loops through each element.

In all this what I am really struggling to understand is why we need the get(0). I have removed it temporarily and the script doesn't work anymore, almost as if this comparison if($(this) == $calledFrom returned false all the time changing the opacity of every thumbnail a to 0.5.


Thread source:: http://www.webmasterworld.com/javascript/4476522.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com