Tuesday, 15 April 2014

Changing onclick attribute using replace with jQuery -



Changing onclick attribute using replace with jQuery -

do know best way replace string within onclick attribute ? i need current value , replace text within parameters.

exemple, i have link:

<a href="#" onclick="myfunction('parameter1a','parameter1b')">my link</a>

and want this:

<a href="#" onclick="myfunction('parameter2a','parameter2b')">my link</a>

in other words, want this:

$('a').attr('onclick', $(this).attr('onclick').replace('1', '2'));

and know can this, need dynamic retreiving values of current element:

$("a").attr('onclick', "myfunction('parameter2a','parameter2b')");

finally working when made simple demo: http://jsfiddle.net/gkwhh/4/

thank solutions !

$('a[onclick]').attr('onclick', function(i, v){ homecoming v.replace(/1/g, '2'); });

http://jsfiddle.net/cj9j7/

if need more dynamic not utilize onclick attributes, changing onclick attributes hackish, can utilize click method instead.

var param = 1; $('a').click(function(){ // ... if ('wildguess') { param = 1; } else { param++; } })

jquery attributes replace onclick attr

No comments:

Post a Comment