JQuery: How to make $ (this) selector?

How do I make the $ (this) selector?

jQuery

$('.parent').find(".child1").html($(this).parent().find('.info').html());

      

for example HTML

 <div class= "parent>
 <div class="child1"></div>
 <div class="child2"></div>
 <div class="child2"></div>
 <div class= "info"></div>
 </div>

      

+3


source to share


1 answer


Pass the function to your method html

and return the desired text you want:

$('.parent').find(".child1").html(function () {
    return $(this).parent().find('.info').html();
});

      

JSFiddle Demo .




From the official jQuery documentation :

.html( function )

: a function that returns the HTML content to install. Gets the index position of the element in the set and the old HTML value as arguments. jQuery frees the element before calling the function; use the oldhtml argument to link to previous content. Within a function, refers to the current item in the set. this

+5


source







All Articles