Dynamically changing a javascript onclick value

Posted on 2008-11-11
Last Modified: 2012-05-05
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="" 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

Question by:Thread7
    LVL 7

    Expert Comment

    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

    LVL 15

    Expert Comment

    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

    LVL 1

    Author Comment

    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;
    LVL 7

    Accepted Solution

    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
    LVL 7

    Expert Comment

    thisoc = allLinks[i].attributes['onclick'].value; will not work because the attributes collection is not supported correctly on most browsers.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now