Home › Forums › JavaScript › [Solved] Jquery switch view modes without refresh › Re: [Solved] Jquery switch view modes without refresh
January 26, 2011 at 3:31 am
#63928
Member
Not sure why one would build the two loops upfront, its unnecessary bandwidth, the visitor might never switch between the two. The code below will only build the alt loop if required.
grid.php:
echo "grid loop";
?>
list.php:
echo "list loop";
?>
switcher.js:
$(document).ready(function(){
$('#toggle-grid').click(function(){
// hide both loops
$(".view").hide();
// check if loop aleady has been generated
if ($("#grid").html() != "") {
$("#grid").show();
} else { // otherwise fetch loop
$.get("grid.php", function(data) {
$("#grid").html(data).show();
});
}
});
$('#toggle-list').click(function(){
// hide both loops
$(".view").hide();
// check if loop aleady has been generated
if ($("#list").html() != "") {
$("#list").show();
} else { // otherwise fetch loop
$.get("list.php", function(data) {
$("#list").html(data).show();
});
}
});
});