How to click the link to get the page value?

<div class="pagination"> <ul> <li class="control"> <a href="katalog?page=3">Пред.</a> </li> <li> <a href="katalog?page=2">2</a> </li> <li> <a href="katalog?page=3">3</a> </li> </ul> </div> 
  • And cancel the transition? - Stranger in the Q
  • no, just output the value - extrememod
  • transition occurs where to display the value? - Stranger in the Q
  • damn it, then interrupt the transition and maybe an alert - extrememod
  • Well, in general, you have already answered - Stranger in the Q

1 answer 1

 [...document.querySelectorAll("a")].forEach(item => { item.addEventListener("click", (e) => { e.preventDefault(); let link = e.target.getAttribute("href"); let value = link.split("=")[1]; console.log(value); }); }); 
 <div class="pagination"> <ul> <li class="control"><a href="katalog?page=3">Пред.</a></li> <li><a href="katalog?page=2">2</a></li> <li><a href="katalog?page=3">3</a></li> </ul> </div> 

Or so:

 [...document.querySelectorAll("a")].forEach(item => { item.addEventListener("click", (e) => { e.preventDefault(); let link = e.target.getAttribute("href"); let value = link.lastIndexOf("="); let result = link.substring(value + 1); console.log(result); }); }); 
 <div class="pagination"> <ul> <li class="control"><a href="katalog?page=3">Пред.</a></li> <li><a href="katalog?page=2">2</a></li> <li><a href="katalog?page=3">3</a></li> </ul> </div> 

  • and how for example certain container pagination? - extrememod
  • @ extrememod, specifically choose elements a in a pagination container? Or choose a parent when clicking on a ? - meine
  • Click on the link of a specific container - extrememod
  • @extrememod, I apologize, didn’t quite understand the question, but I dare to assume that ... querySelectorAll(".pagination a")... - meine
  • why do you convert a collection of elements into an array, if it already has a forEach method? - Vadim Leshkevich