1

I have a dynamical hyperlink generated via jQuery. And I don't know how to get value of this element:

<a href="?t-action=' + data[i].pk_id + '" class="transfer">'+ '\ <img src="../images/like.png"...

But if i'm using alert($(this).attr('href')); The output is ?t-action=12345 Is it posible to get only 12345?

Klapsius
  • 3,273
  • 6
  • 33
  • 56
  • 4
    you can use split then get second data like `$(this).attr('href').split('=')[1]` assuming the value is always like that – guradio Jan 11 '17 at 10:43
  • Possible duplicate of [Get url parameter jquery Or How to Get Query String Values In js](http://stackoverflow.com/questions/19491336/get-url-parameter-jquery-or-how-to-get-query-string-values-in-js) – CBroe Jan 11 '17 at 10:46

2 Answers2

2

You can add an attribute to a tag. In this case I use data-.

like this :

<a href="?t-action=' + data[i].pk_id + '" data-number="+data[i].pk_id+" class="transfer">

Then you can get than value by

$(this).data('number');

You can change the tag data-number to something else you prefer. Then update string inside .data() to be the same.

If you use data-foo in your jquery have to be $(this).data('foo');.

https://api.jquery.com/jquery.data/

Natsathorn
  • 1,530
  • 1
  • 12
  • 26
0

I sugest you to use data attributes.

<a href="?t-action=' + data[i].pk_id + '" data-pkid="data[i].pk_id" class="transfer">'+ '\ <img src="../images/like.png"...

Then, to output id use this:

alert($(this).data('pkid'));
Marcelo Sader
  • 331
  • 4
  • 12