Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse onclick="setLocation(...)" with jQuery / prototype

I have this tag in example:

<button onclick="setLocation('http://mysite.com/checkout/cart/add/product/17/')" />

What I need to do is initiate ajax request on button click. I need URL segment "product/17" from above appended to my ajax url. Any clever ways to extract this without using regex? HTML is not changable - however I can modify the URL, and in that case I would need to extract the exact URL from onclick attribute.

So I either need URL or "product/17" extracted from onclick attribute, hopefully without using regex.

Thanks

like image 447
srgb Avatar asked Oct 17 '12 11:10

srgb


Video Answer


1 Answers

So, if no RegExp usage, and no criteria for parsing the string, here is the simplest solution:

var str = ​$("button").attr("onclick"​​​​);
str = str.substring(str.lastIndexOf("add/") + 4, str.length - 3);​
like image 163
VisioN Avatar answered Sep 25 '22 07:09

VisioN