This was a relatively difficult one to explain in the title, so this is a further explanation.
Say I extend JQuery by doing something like the following:
$.fn.extend({
Example:function(){
$(this).html($(this).html());
}
});
$(document).ready(function(){
$(".element").Example();
});
<div class="element">Element 1</div>
<div class="element">Element 2</div>
Element 1
Element 1
The issue is because this
is a collection of all the elements in the selector. Getting the html()
of a collection will only return the HTML of the first element in the set. Therefore your code sets the HTML of all elements to match the first.
To fix this you can either use an each
call to change the context of this
to each individual element:
$.fn.extend({
Example: function() {
return $(this).each(function() {
$(this).html($(this).html() + ' foo');
});
}
});
$(document).ready(function() {
$(".element").Example();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="element">Element 1</div>
<div class="element">Element 2</div>
Alternatively, you can provide a function to html()
which will be applied to each element in turn:
$.fn.extend({
Example: function() {
return $(this).html(function(i, html) {
return html + ' foo';
});
}
});
$(document).ready(function() {
$(".element").Example();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="element">Element 1</div>
<div class="element">Element 2</div>