3

Do someone know what is the best way to replace some string inside a onclick attribute ?
I need to get the current value and replace some text inside parameters.

Exemple,
I have this link:

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

And I want this:

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

In other words, I want something like this:

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

And I know I can do this, but I need something dynamic retreiving the values of current element:

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

Finally it working when I made a simple demo: http://jsfiddle.net/GkWhh/4/

Thank you for your solutions !

5
  • Why do you "need something more dynamic?" Commented Jan 16, 2013 at 19:03
  • You just need to know what the paramters are you need to change and create a conditional statement within the click function to pass the parameters to your custom myFunction(par1, par2) Commented Jan 16, 2013 at 19:03
  • Why do you need to do this in first place? How is this layout being created and why can't you change it? Overall, this is a wrong approach Commented Jan 16, 2013 at 19:11
  • @Alexander - Do you have a better approach ? Commented Jan 16, 2013 at 21:49
  • I made a simple demo on jsFiddle. But finally it working! I will look closer to my real code to see what is missing. Before, when I added some objectives (see demo) the link to add Solution didnt update the parameters. Thank you for your help ! Commented Jan 16, 2013 at 22:02

3 Answers 3

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

http://jsfiddle.net/cj9j7/

If you need something more dynamic do not use onclick attributes, changing onclick attributes is hackish, you can use click method instead.

var param = 1;
$('a').click(function(){
   // ...

   if ('wildguess') {
     param = 1;
   } else {
     param++;
   }
})
Sign up to request clarification or add additional context in comments.

6 Comments

I would also change the selector to a[onclick]
It is weird, I see that it's working on your jsFiddle. But for me the console show me : Uncaught TypeError: Object function onclick(event) { myfunction('parameter1a','parameter1b') } has no method 'replace'
@ShadGagnon That's because some of your a elements don't have onclick attributes, I have updated the selector.
I have updated the selector... But the problem is still here. I tried to alert the onclick attr like this alert($(this).attr('onclick')) before the return v.replace(/1/g, '2') and it show me my function correctly. The console still show : Uncaught TypeError: Object function onclick(event) { myfunction('parameter1a','parameter1b' } has no method 'replace'
@ShadGagnon Can you provide a demo on jsfiddle?
|
1

sounds like a really bad idea but anyway - you can access the string value of the onlick attribute using something like that:

$('a').each(function() { this.attributes.onclick.nodeValue = this.attributes.onclick.nodeValue.replace('1', '2'); })

Comments

0

You can do this: http://jsfiddle.net/SJP7k/

var atr = $('a').attr('onclick');
var str = atr.split('1');
var natr = str.join('2');
$('a').attr('onclick',natr);

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.