Forum Moderators: open
<a class="nextClassified" href="$next_link" style="display: none">next</a> // $frag is created via PHP before this script; basically the same as
// $next_link, but created outside of #classifiedList
function loadFragment(viewstyle) {
var otherlist = 'grid';
if (viewstyle == otherlist) otherlist = 'list';
var url = '$frag' + '&v=' + viewstyle;
$('#fragLoad').load(url + ' #fragLoad');
// setCookie is a separate function I built a long time ago, using document.cookie
setCookie('viewstyle', viewstyle, 1);
// Here's where I'm trying to change v=list or grid
// this throws an error that index can't run on undefined
if ($('a.nextClassified:last').attr('href').index('v=') !== -1) {
$('a.nextClassified:last').attr('href').replace(/v=(list|grid)/, 'v=' + viewstyle);
// Check to see if I make it this far... spoiler, it doesn't
alert('1');
}
// test to see if anything can be read, but both alert "undefined"
// which makes NO sense, because this works fine in the infinite scroll:
// .load($(nextClass + ':last').attr('href') + ' ' + itemID
alert($('.nextClassified:last').attr('href'));
alert($('.nextClassified:last').prop('href'));
// test querySelector instead of jQuery, but "'a.nextClassified:last' is not a valid selector"
// but since I'm in an infinite scroll, I have multiple instances of a.nextClassified
// how else do I only change the last one?
alert(document.querySelector('a.nextClassified:last').getAttribute('href'));
} // $frag is created via PHP before this script; basically the same as
// $next_link, but created outside of #classifiedList
function loadFragment(viewstyle) {
var otherlist = 'grid';
if (viewstyle == otherlist) otherlist = 'list';
var url = '$frag' + '&v=' + viewstyle;
// Here's where I'm trying to change v=list or grid
alert('1 - ' + $('.nextClassified:last').attr('href'));
if ($('.nextClassified:last').attr('href').indexOf('v=') !== -1) {
$('.nextClassified:last').attr('href').replace(/v=(list|grid)/, 'v=' + viewstyle);
alert('2 - ' + $('.nextClassified:last').attr('href'));
$('#fragLoad').load(url + ' #fragLoad');
// setCookie is a separate function I built a long time ago, using document.cookie
setCookie('viewstyle', viewstyle, 1);
} https://ww2.example.com/classifieds/for-sale/guns-hunting/?v=grid&data_b=5&s=20 $('.nextClassified:last').attr('href').replace(/v=(list|grid)/, 'v=' + viewstyle); $var = "whatever";
str_replace('what', 'which', $var); $('.nextClassified:last').attr('href', function(i, originalHref) { return originalHref.replace(/v=(list|grid)/, 'v=' + viewstyle); });
I think it's also :last-child
$('.nextClassified').last().attr('href', function(i, originalHref) {
return originalHref.replace(/v=(list|grid)/, 'v=' + viewstyle);
}
); $('#fragLoad').load(url + ' #fragLoad'); Is the switch between list and grid just a simple CSS toggle, or am I misinterpreting that?
I'm a bit confused by the code. Why are you setting the otherlist variable, for example? You don't seem to be using it.
$('#' + viewstyle + 'Cont').addClass('list_on');
$('#' + otherlist + 'Cont').removeClass('list_on'); $('#fragLoad').load(url + ' #fragLoad');
And this... why are you tacking the space and #fragLoad anchor onto the URL?
It's difficult to get a firm grasp without seeing all the code.