javascript: change the onClick event dynamically

Posted on 2009-12-22
Last Modified: 2013-11-18
The onClick event is dynamically created at load time.  Inside the onclick event is a value similar to the following  table_field_1.  Each time a new row is created, the onclick event needs to have table_field_1 changes to table_field_2, table_field_3 and so on.

The code I included has issues because the getAttribute("onCick") returns the keyword function.  If I could get it to return just the actual content of the onClick, I think it could work.

Need help shaping this solution.
if(els[el].id.indexOf("anchor") > -1)


				//sample code that needs to be manipulated...

				//['WebForm']." & fieldelement.getAttribute("id")  & ",'anchor1','MM/dd/yyyy'); return false

				//retrieve the onClick event as text and determin the begin and end point for the number following the last underscore

				//var starttargpos = els[el].getAttribute("onClick").lastIndexOf("_") 

				//var stoptargpos =  els[el].getAttribute("onClick").indexOf(",'anchor1") 


				//parse out the text to the left and the text representing the number to be changes

				//lefttext = els[el].getAttribute("onClick").substring(0,starttargpos - 1)

				//targettext = els[el].getAttribute("onClick").substring(starttargpos,stoptargpos)


				//increment the number and retrieve the text to the right

				//newtargettext = Number(targettext) + 1

				//righttext = els[el].getAttribute("onClick").substring(stoptargpos + 1)


				//reassemble the left, target and right together and assign to the onClick attribute

				//newonclick = lefttext + newtargettext + righttext


				//els[el].getAttribute("onClick") = els[el].getAttribute("onClick")



Open in new window

Question by:KeithMcElroy
    LVL 3

    Expert Comment

    have you tried seeing what getAttribute("onCick").value returns?
    LVL 3

    Assisted Solution

    Actually not sure that's any help at all.
    taken from
    NodeList elementsByTagName = browser.getDocument().getElementsByTagName("input");
            for (int i = 0; i < elementsByTagName.getLength(); i++)
                HTMLElement element = (HTMLElement) elementsByTagName.item(i);
                Attr onclick = element.getAttributeNode("onclick");
                System.out.println("onclick = " + onclick.getValue());

    Open in new window

    LVL 14

    Accepted Solution

    I'm confused by the description and the example, they don't seem to jive together. But does this help any.

    <a id="clicker" onclick="foo('table_field_1')">click me</a>
    <script language="javascript">
        var tableCount = 1;
        function foo(bar) {
            document.getElementById("clicker").onclick =
                function(event) { foo("table_field_"+tableCount); };

    Open in new window

    LVL 40

    Assisted Solution

    you need to change the event like this
    document.getElementById('tr1').onclick = newFunction;

    But in case you want to use the function with params then

    document.getElementById('tr1').onclick = new Function(someFunctionName(input + counter + ));


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Javascript: Crash Course

    Javascript is not only emerging as the most important language to know, it's flexibility is unique. With Javascript being deployed on the web, mobile and even the desktop this is the one language that all technologists need to know.

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Before we dive into the marketing strategies involved with creating an effective homepage, it’s crucial that EE members know what a homepage is. In essence, a homepage is the introductory, or default page, of a website that typically highlights the …
    The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
    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)

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now