2

In the following markup on a page I want to extract out the following and put them in seperate variables.

1- In the onclick attribute get the value after the "ProductID" and put it in its own variable. "ProductID" So in this case it would be 318

2- In the onclick attribute get the value after the "Orig_price" and put it in a variable, "Orig_Price" So in this case it would be 22.95

3- In the onclick attribute get the value after the "width" and put it in a variable, "width" So in this case it would be 330

4- In the onclick attribute get the value after the "height" and put it in a variable, "height" So in this case it would be 300

<a href="javascript:void(0);" onclick="window.open('/BulkDiscounts.asp?ProductID=318&ProductCode=' + escape('LB30X40ES') + '&Orig_Price=22.95', 'Discounts', 'scrollbars,status,resizable,width=330,height=300');"><img src="/v/vspfiles/templates/100/images/buttons/btn_quantitydiscounts.gif" border="0" align="absmiddle"></a>
2
  • You want to get those values out of the DOM of the current page? Or do you have a string containing <a href=…></a>? Commented Jun 10, 2010 at 15:15
  • get the values out of the DOM Commented Jun 10, 2010 at 15:18

3 Answers 3

1

UPDATED DEMO:

DEMO: http://jsbin.com/axuce3/3 SOURCE: http://jsbin.com/axuce3/3/edit

var pieces = $('a').attr('onclick').toString().split('?')[1].split('=');
var parts = [];
for (var i = 0; i < pieces.length; i++) {
    var value = parseFloat(pieces[i]);
    if (!isNaN(value)) parts.push(value);
}
alert( 'ProductID=' + parts[0] + 'Orig_price=' + parts[1] + 'width=' + parts[2] + 'height=' + parts[3]);
Sign up to request clarification or add additional context in comments.

1 Comment

I checked this code and it works great!!! of course I removed the alert code and used the variables as needed. I did have to mod the code or it targeted the first "onclick" on the page which this was not. I added this to the top of your code. Thanks A LOT!!! $("a[onclick*='/BulkDiscounts.asp?ProductID=']") .attr('id','quantity_dis'); And changed the first line of you code to this var pieces = $('#quantity_dis').attr('onclick').toString().split('?')[1].split('=');
1

Please forget my previous answer, it was wrong (to test it, I assigned an id to the wrong element). You can get the value of the onclick attribute using getAttribute. For testing purposes, I changed your example to

<a href="javascript:void(0);" id="test" onclick="window.open('/BulkDiscounts.asp?ProductID=318&ProductCode=' + escape('LB30X40ES') + '&Orig_Price=22.95', 'Discounts', 'scrollbars,status,resizable,width=330,height=300');"><img src="/v/vspfiles/templates/100/images/buttons/btn_quantitydiscounts.gif" border="0" align="absmiddle"></a>

Now document.getElementById("test").getAttribute("onclick") returns

window.open('/BulkDiscounts.asp?ProductID=318&ProductCode=' + escape('LB30X40ES') + '&Orig_Price=22.95', 'Discounts', 'scrollbars,status,resizable,width=330,height=300');

Now you can get the values using

var theString = document.getElementById("test").getAttribute("onclick");
var ProductID = theString.match(/ProductID=(\d*)/i)[1];
var Orig_Price = theString.match(/Orig_price=([\d\.]*)/i)[1];
var width = theString.match(/width=(\d*)/i)[1];
var height = theString.match(/height=(\d*)/i)[1];

(code is stolen from Tambourine's answer, so please give him the credits ;).

1 Comment

I am not sure if this would work as well as I didn't try it but I would assume it would. Thanks for the effort!!!
1

Try this code:

var string = $('a').attr('onclick') + ""; // to be sure var string is string
ProductID = string.match(/ProductID=(\d*)/i)[1];
Orig_Price = string.match(/Orig_price=([\d\.]*)/i)[1];
width = string.match(/width=(\d*)/i)[1];
height = string.match(/height=(\d*)/i)[1];

9 Comments

When i try your code and put a alert(string); at the end i get "undefined" in the alert box so perhaps you cannot reference the onclick attr?
is there a way to get the whole <a> anchor and them do a string match?
@user: Yes, you can get the string; in my previous answer I was wrong due to assigning an id to the wrong element.
@user: My new answer is below. Sorry for the confusion.
Strange, but it works for me. I added + "" to be sure that string is string type. Try it!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.