A user called “mu” as provide me the following answer that I really like.
However, I do have some questions here, and I hope anyone could help me out.
Given the following:
function() {
var $this = $(this);
$this.data('orig', $this.html()).html('<a href="blabla.org">go here</a>');
},
function() {
var $this = $(this);
$this.html($this.data('orig')).removeData('orig');
}
1)
Why do we have the need, and what does it do:
var $this = $(this) – Why can’t we use $(this) all over the place?
2) Why do we need the ‘orig’ thing there?
Please, 🙂 provide as much detail as possible, really, I’m quite new on all this.
Thanks a lot in advance,
MEM
There is no need to use
$thisinstead of$(this), but it will be faster. You are making the call$(this)once and storing the result in a variable instead of doing the call over and over again.Note that
$(this)is a call to the function$with the value ofthisas parameter, while$thisis just the name of a variable. The function call returns a jQuery object that contains a list of the one element thatthisrefers to, so that’s what’s stored in the variable.The string
'orig'is an identifier for the piece of data that you are storing. Each piece of information that you store using thedatamethod needs an identifier.