0

Hello I have a doubt about $(this) in jQuery. I was doing an exercise, at the end of the script you can see:

$(this).before(quote);

Can I consider $(this) as temporary variable that contains the value of that loop? In generally how can I consider $(this)?

$(document).ready(function() {
  $('span.pq').each(function() {
    var quote = $(this).clone();
    quote.removeClass('pq');
    quote.addClass('pullquote');
    $(this).before(quote);
  }); //end each
}); // end ready
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
franco
  • 15
  • 4
  • `this` = the `span.pq` Element. `$(this)` = the `span.pq` element wrapped in a jQuery object so you can call jQuery methods on that element. – Rory McCrossan Apr 07 '17 at 07:46

1 Answers1

0

$('span.pq') will return you the list of elements which is basically an array of elements. $(this) will be element of $('span.pq') array of elements.

$(this) will be equal to $('span.pq')[index] // say index is an variable of each loop.

murli2308
  • 2,976
  • 4
  • 26
  • 47
  • OK I understand you. Now after using .clone() in the var quote I will have the original string plus its copy, right? But with removeClass it will remove .pq to both?? The problem is that when I use source page in the browser the html code is the same, so I am not sure how it works. THanks – franco Apr 07 '17 at 08:22