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;

Thanks.
---HTML---
<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

LVL 1
Thread7Asked:
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.

alexpercsiCommented:
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:

allLinks[i].onclick=function(){alert('zvc');function1();}

Open in new window

0
quincydudeCommented:
You may like to check out this code about the technique
<html>
<head>
<script language="javascript">
function saybye() 
{
alert("bye!");
}
 
function change()
{
var me = document.getElementById('field2');
alert(me.attributes['onclick'].value);
me.onclick = function() {saybye()};
}
</script>
</head>
<body>
 
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>
 
 
</body>
</html>

Open in new window

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

thisoc=allLinks[i].onclick;

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;
0
alexpercsiCommented:
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:
https://addons.mozilla.org/en-US/firefox/addon/1843

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
0

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
alexpercsiCommented:
thisoc = allLinks[i].attributes['onclick'].value; will not work because the attributes collection is not supported correctly on most browsers.
0
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
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.