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>
The scope of
thishas changed. If you want to keep a reference to the original element, create a variable at theclickscope and thesuccesshandler will include it in its closure: