I want the following loadContent function to load the target div only if the link clicked has a certain class. This is what I’ve come up with so far:
function loadContent(targetDIV, sourceURL) {
if ( $(this).hasClass("done") ) {
$(""+targetDIV+"").load(""+sourceURL+"");
}
}
<a href="javascript:loadContent('#nav', 'news.php');" class="done">News</a>
The function works without the if statement, but I just can’t seem to get the hasClass to work with the if – any thoughts? Thanks!
Change it to the following, and it should work:
By using
.call(), you’re setting the value ofthisin theloadContentfunction to the current element.And because we used
onclick,thisin the inline handler will give you that reference to pass on.