Get onclick string using jQuery

I had to extract the second parameter (array) from the onclick attribute in the image, but jQuery just returned the onclick function and not its string value as expected. So I had to use my own method.
A quick search says that it can work with some browsers, such as FF, but not with IE. I am using Chrome.

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

I thought this would work, but this is 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)

It 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 any other way to get the second parameter?

+3
source share
2 answers

Why not store it in a 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(',');
+1

UPDATE

span onclick. , (firefox 7, JQuery 1.5.1). .

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

Source: https://habr.com/ru/post/1774964/


All Articles