Is it possible to turn a function into plain text, alter it, and then reset it back to a function?

Here is a sample of what I’m trying to achieve.

<script>
function test()
{
  onclick_old = document.getElementById('testbutton').onclick;
  onclick_new = onclick_old.replace('this','that');
  document.getElementById('testbutton').setAttribute('onclick',onclick_new);
}
</script>

<input type="button" onclick="test();" value="change onclick" />
<input id="testbutton" type="button" onclick="alert('this');" />

But when running this, it says

TypeError: onclick_old.replace is not a function

Which makes sense, onclick_old in this case isn’t a string, but I want to treat it as such. How can I get around this?

Thanks in advanced :smiley:

[quote=“wh33t, post:1, topic:228454, full:true”]
Which makes sense, onclick_old in this case isn’t a string, but I want to treat it as such. How can I get around this?[/quote]

Use .getAttribute(“onclick”) instead

1 Like

Thank you :smiley: much appreciated.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.