Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery How to extract value from href tag?

Tags:

jquery

I am new to JQuery.

If I have the following tag. What is the best JQuery method to extract the value for "page" from the href.

<a href="Search/Advanced?page=2">2</a> 

Malcolm

like image 398
Malcolm Avatar asked May 16 '09 11:05

Malcolm


2 Answers

The first thing that comes to my mind is a one-liner regex:

var pageNum = $("#specificLink").attr("href").match(/page=([0-9]+)/)[1]; 
like image 139
Matchu Avatar answered Sep 30 '22 04:09

Matchu


I see two options here

var link = $('a').attr('href'); var equalPosition = link.indexOf('='); //Get the position of '=' var number = link.substring(equalPosition + 1); //Split the string and get the number. 

I dont know if you're gonna use it for paging and have the text in the <a>-tag as you have it, but if you should you can also do

var number = $('a').text(); 
like image 21
Kenny Eliasson Avatar answered Sep 30 '22 06:09

Kenny Eliasson