Reputation: 6365
In my code i'm using the following two lines from a previous question of mine here.
$(this).removeClass('page larger').addClass('current');
$(this).siblings().removeClass('current').addClass('page larger');
If I place these anywhere outside success: It works fine. But if I place them inside success: they wont work. I need them to work only on success:. How can I solve this?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>PHP, jQuery search demo</title>
<style type="text/css">
.current{
padding:5px;
border:1px solid #000000;
background-color:white;
cursor:pointer;
}
.page{
padding:5px;
border:1px solid #000000;
background-color:red;
cursor:pointer;
}
</style>
<script type="text/javascript" src="http://localhost/jQuery1.8.3.js"></script>
<script type="text/javascript">
$(function() {
$('.page').live('click', function() {
var menus =($(this).attr("menus"));
var data = menus;
if(data) {
//alert (data);
// // ajax call
$.ajax({
type: "GET",
url: "pages.php",
data: data,
dataType: "json",
cache: false,
beforeSend: function(html) {
$(".pageLoading").html("Fetching new posts...");
},
success: function(page_data){
$(".posts").empty();
$(".pageLoading").empty();
$(".posts").html(page_data['articles']);
console.log (page_data['articles']);
$(this).removeClass('page larger').addClass('current');
$(this).siblings().removeClass('current').addClass('page larger');
},
});
}
return false;
});
/////////////////////////////////////////////////////////////////////////
});
</script>
</head>
<body>
<div class="posts">These are the existing posts</div>
<br />
<div class="navigation clearfix">
<div class="menusHolder">
<span menus="in/consumer/p22" class="current">1</span>
<span menus="in/consumer/p19" class="page larger">2</span>
<span menus="in/consumer/p15" class="page larger">3</span>
<span menus="in/consumer/p10" class="page larger">4</span>
</div>
</div>
<br />
<div class="pageLoading"></div>
</body>
</html>
Upvotes: 2
Views: 90
Reputation: 42497
The scope of this
has changed. If you want to keep a reference to the original element, create a variable at the click
scope and the success
handler will include it in its closure:
$(function() {
$('.page').live('click', function() {
var menus =($(this).attr("menus"));
var data = menus;
var $that = $(this); // <- add this
if(data) {
//alert (data);
// // ajax call
$.ajax({
type: "GET",
url: "pages.php",
data: data,
dataType: "json",
cache: false,
beforeSend: function(html) {
$(".pageLoading").html("Fetching new posts...");
},
success: function(page_data){
$(".posts").empty();
$(".pageLoading").empty();
$(".posts").html(page_data['articles']);
console.log (page_data['articles']);
// change $(this) to $that. success handler is a closure that will include $that
$that.removeClass('page larger').addClass('current');
$that.siblings().removeClass('current').addClass('page larger');
},
});
}
return false;
});
/////////////////////////////////////////////////////////////////////////
});
Upvotes: 3
Reputation: 2051
The context option will do the trick, as described here.
Simply add it on to your ajax call as follows:
$.ajax({
type: "GET",
context: this,
url: "pages.php",
data: data,
...
success: function() {
$(this).removeClass('page larger').addClass('current');
$(this).siblings().removeClass('current').addClass('page larger');
}
});
Upvotes: 3
Reputation: 850
Use,
$('.page').live('click', function() {
var $that = $(this);
// use that
});
In ajax scope, this refer to ajax object.
Upvotes: 3