Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript [Solved] Jquery switch view modes without refresh Re: [Solved] Jquery switch view modes without refresh

#63928
stefanbar
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();
});
}
});
});