Dynamically changing a javascript onclick value

I have tried unsuccessfully to dynamically change the onlick value that is currently in an <a> tag.  It seems like the code below should work but I get an error at: thisoc =  allLinks[i].onclick;

<a href="http://www.mysite.com/somepage.php?id=381" onclick="function1();">Some text to click</a>
---Javascript in my function---
    allLinks = document.getElementsByTagName('a');
	for (var i = 0; i < allLinks.length; i++) {
	    thislink = allLinks[i].href;
            thisoc =  allLinks[i].onclick;  //This statement produces an error
            if(thislink.indexOf('somepage.php') > 0) {
              //Need to insert our own function call in the onclick part
              thisoc.replace(/function1/gi, "alert('zvc');function1");
              allLinks[i].onclick = thisoc;

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The problem is that calling thisoc=allLinks[i].onclick will not produce a string value in thisoc, it will produce a reference to the function.

You can try something like this:


Open in new window

You may like to check out this code about the technique
<script language="javascript">
function saybye() 
function change()
var me = document.getElementById('field2');
me.onclick = function() {saybye()};
Field2: <input type="text" id="field2" onclick="alert('hi');">
<br /><br />
Click the button below to change method of onclick.
<br />
<button onclick="change();">Change</button>

Open in new window

Thread7Author Commented:
Hmm, I don't think the statement


even creates a reference to the function.  My code stops running at that line.  I tried putting this line instead but it also stops my code at this point.

thisoc = allLinks[i].attributes['onclick'].value;
Have you tried the line I had suggested?

As an alternative, you could try downloading firebug, a developer plugin for mozilla firefox, and using it to determine the specific error that occurs.

You can get it here:

Don't forget to enable it before running your code by clicking the bug icon on the lower right corner of your browser and checking the Console and Script checkboxes.

Best Regards,
Alex Percsi

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
thisoc = allLinks[i].attributes['onclick'].value; will not work because the attributes collection is not supported correctly on most browsers.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.