Geek Grid
Geek Grid

Reputation: 207

Get href string with more than one class using jQuery

I have this link...

<a class="class sub1 class2" href="test.asp?Code=2D3824&ID=1">

I need to get the ID value in the href. I tried:

jQuery("a.class.sub1.class2[href$='2D3824']").text();

To try and get a string to break up, but it comes back empty. Any ideas?

Upvotes: 0

Views: 62

Answers (2)

adeneo
adeneo

Reputation: 318202

If the string always has the same format :

jQuery("a.class.sub1.class2[href*='2D3824']").attr('href').split('=')[2];

FIDDLE

To get the value based on the querystring key with a regex:

jQuery("a.class.sub1.class2[href*='2D3824']").attr('href').match(/\&ID\=(.*?)$/)[1];

Upvotes: 1

Sam Tyson
Sam Tyson

Reputation: 4616

You need to get the property for the href, not the text of the anchor, and you also need to use the contains selector, *=.

jQuery("a.class.sub1.class2[href*='2D3824']").prop('href').split('=')[2];

Upvotes: 0

Related Questions