3

I had to extract the 2nd parameter (array) from an onclick attribute on an image, but jQuery just returned a function onclick and not its string value as expected. So I had to use a native method.
A quick search says it may work some browsers like FF, but not IE. I use Chrome.

<img src="path/pic.png" onclick="funcName(123456,[12,34,56,78,890]);" />

I thought this would work, but it does not:

var div = $('div_id');
var onclick_string = $(div).find('img').eq(0).attr('onclick');
var onclick_part = $(onclick_string).match(/funcName\([0-9]+,(\[.*\])/)[1]; // for some reason \d doesnt work (digit)

This works

var div = $('div_id');
var onclick_string = $(div).find('img')[0].getAttributeNode('onclick').value;
var onclick_part = $(onclick_string).match(/funcName\([0-9]+,(\[.*\])/)[1]; // for some reason \d doesnt work (digit)

Is there another way of getting the 2nd parameter ?

2 Answers 2

1

Why not store it in the data property?

<img src="path/pic.png" onclick="funcName(123456);" data-mydata='12,34,56,78,890' />

var div_data = $('div_id').data('mydata').split(',');
Sign up to request clarification or add additional context in comments.

Comments

0

UPDATE

I used the following code to loop through a collection of span tags and output their onclick strings. it's hacky but it works (firefox 7, JQuery 1.5.1). You can even override the string value.

$("span").each(function(index)
{
  alert( $(this)[0].attributes.onclick.nodeValue );
});

Comments

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.