$(document).ready(function() {
$(".po").click(function(){
var po = $(this).text();
var dataString = 'po='+ po;
$.ajax
({
type: "GET",
url: "projectitems.php",
data: dataString,
cache: false,
success: function(html)
{
$(this).closest(".resultsItems").html(html);
}
});
});
});
The line $(this).closest(".resultsItems").html(html);
what exactly is (this) referring to? I'm trying to append the returned ajax result to a <td>
called .resultsItems but only to the one below the intial clicked selector? Is this possible?
Just to make it clear i'm not asking what (this) means in jQuery, i'm asking what (this) is referring to in my code above!
$(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this . Calling jQuery a second time (which is a mistake) on the result of $(this) returns an new jQuery object based on the same selector as the first one.
$(this) is a jQuery object and this is a pure DOM Element object. See this example: $(".test"). click(function(){ alert($(this). text()); //and alert(this.
Difference between this and $(this)The this Keyword is a reference to DOM elements of invocation. We can call all DOM methods on it. $() is a jQuery constructor and in $(this), we are just passing this as a parameter so that we can use the jQuery function and methods.
this would refer to the current DOM element h1 which invoked the event. Enclosing it with a $ makes it a jquery object. So $(this) basically refers to the jquery h1 object . So $(this) = $('h1') where h1 is the event which triggered the event.
this
refers to the $.ajax()
settings object. To get what you want, you'll need to maintain this
by using the context
option like this:
$.ajax({
context: this,
type: "GET",
url: "projectitems.php",
data: dataString,
cache: false,
success: function(html) {
$(this).closest(".resultsItems").html(html);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With