Search code examples
javascriptjquerycssclickhref

Jquery - How to get page number from link href and use as class


I am working on a custom filtering system and for one of my functions, I need to get the page number from href of the clicked link. My URL looks like this--

mysite.com/page/2/?address&s=Search.....

I've tried the following code but it doesnt seem to be working.

$('.goforward').click(function() {
  var href = $(this).attr('href');
  var selector = $(this).attr('page'); // make selector equal to the value we put in 'page'
  var pageNum = $(this).attr("href").match(/page="([0-9]+)"/)[1]; // get page number
  var fdd1 = $('[name="rating_score"]').val();
  var fdd2 = $('[name="value_score"]').val();

  // Create div with class iso-pageNum
  $('.isotope').append($('<div class="iso-' + pageNum '">').load(href + ".html .isotope > *", function() {
    select1f();
    select2f();

    if ($("select[name=rating_score] option:selected").val() == 'allr') {} else {
      // Select recent created div
      $('.isotope .iso-' + pageNum).children('div:not(' + fdd1 + ')').hide();
    }
  }));
  return false;
}); 

Solution

  • You can find the page number like following.

    var url = 'mysite.com/page/2/?address&s=Search';
    var arr = url.split('/');
    var pageNum = arr[arr.indexOf('page') + 1];
    
    console.log(pageNum)