3

I do the following to get the parameter of an anchor tag, but there's probably some better way to do it:

$('a').has('.icon-thumbs-up').click(function(e) {
    var local = {};
    e.preventDefault();
    local.href = $(this).attr('href');
    local.X = local.href.indexOf('GuessID=',0) + 8;
    local.Y = local.href.indexOf('&',local.X);
    local.GuessID = local.href.substr(local.X,local.Y-local.X);

Q: Is there some sexy jQuery way of getting 123 from href="page.htm?GuessID=123"?

Phillip Senn
  • 46,771
  • 90
  • 257
  • 373
  • Someone posted it and then removed it, not sure why. I'd have a look at http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript if you want something more generic. – James Montagne Aug 24 '12 at 21:33
  • 1
    As [sexy](https://github.com/allmarkedup/jQuery-URL-Parser) as it gets. – MalSu Aug 24 '12 at 21:33

1 Answers1

4

Why use the overhead of jquery when it's so easy to do naturally with javascript? Of course this will work and it's making an assumption that the <a> has a query string.

$('a').has('.icon-thumbs-up').click(function(e) {

     var querystring = $(this).prop('href').split("?")[1]; //GuessID=123&someother=123
     var values = querystring.split("&"); 
     var first =  values[0];  // GuessID=123
     var second = values[1];  // someother=123
});
Gabe
  • 49,577
  • 28
  • 142
  • 181