views:

82

answers:

3

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>
+1  A: 

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];
tambourine
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.
Marcel Korpel
ok so now i am confused, any code you could show me? thx
@user: My new answer is below. Sorry for the confusion.
Marcel Korpel
Strange, but it works for me. I added `+ ""` to be sure that string is string type. Try it!
tambourine
Worked for me, too, though you should really prepend those variables with `var` to define them locally, otherwise you pollute the global scope (and if another function, e.g. in an extension, uses `height`, things will be horribly wrong).
Marcel Korpel
@Marcel: You absolutely right. It works with `var`, but not without.
tambourine
Oh, the problem was `string`, but you should use `var` before every variable. And the `+ ""` is not necessary.
Marcel Korpel
Sorry for not responding sooner but I need to check it again and then comment. I think I know why it didn't work the 1st time. Thx for the time
+1  A: 

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 ;).

Marcel Korpel
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  A: 

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]);
aSeptik
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 thisvar pieces = $('#quantity_dis').attr('onclick').toString().split('?')[1].split('=');