0

I have this piece of code here and I am trying to combine it together to be more semantic.

$(jsSearchPageNr[pageNo]).show();
$(jsSearchPageNr[pageNo - 2]).show();
$(jsSearchPageNr[pageNo - 1]).show();

What I am trying to do is something like this

$(jsSearchPageNr[pageNo - 1], jsSearchPageNr[pageNo - 2], jsSearchPageNr[pageNo]).show();

I have also tried this

 $([jsSearchPageNr[pageNo - 1], jsSearchPageNr[pageNo - 2], jsSearchPageNr[pageNo]]).show();

jsSearchPageNr variable looks like this

var jsSearchPageNr = $('.search-results-wrapper__pagination--' + className + ' .js-search-page-number');

but this gives me an error Uncaught TypeError: Cannot read properties of undefined (reading 'style')

Cheers!

in2d
  • 544
  • 9
  • 19
  • It would help if we could see what jsSearchPageNr looks like – Carsten Løvbo Andersen Apr 21 '22 at 11:55
  • Thx I edited my question. @CarstenLøvboAndersen – in2d Apr 21 '22 at 12:00
  • As `jsSearchPageNr` = `$(...)` you don't need to rewrap it `$(jsPgNr)` just `jsPgNr.eq(pageNo).show()`. It looks like the `undefined style` issue is because there aren't jsPgNr items with those indexes. – freedomn-m Apr 21 '22 at 14:01
  • From [this answer](https://stackoverflow.com/a/323969/2181514) and [this answer](https://stackoverflow.com/a/9056661/2181514), you can combine jquery collections using `.add()`: using your original [i] code: `$(jsSearchPageNr[pageNo - 1]).add(jsSearchPageNr[pageNo - 2]).add(jsSearchPageNr[pageNo]).show();` should do the trick. – freedomn-m Apr 21 '22 at 14:03

0 Answers0