dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: Need help with changing a button's onclick

  1. #1
    Join Date
    Nov 2009
    Posts
    13

    Need help with changing a button's onclick

    I'm baffled as to why I'm getting a "null" when trying to assign a new onclick to a form button after it's clicked. When I troubleshoot I'm using alerts. Here is my code:

    Code:
    alert(document.getElementById(formName).onclick);
    // this first alert yeilds: function onclick(event) { javascript:readTextFile("81","plus"); }
    			   document.getElementById(formName).onclick = 'javascript:readTextFile("81","minus")';
    			   alert(document.getElementById(formName).onclick);
    // this second alert yields: null
    Am I assigning the onclick incorrectly?

  2. #2
    Join Date
    Nov 2009
    Posts
    13
    I tried adding ".value" after "onclick" but that didn't work either. It does not change it at all when I do that. I get the same as the first alert. Why won't it change the onclick???
    Last edited by Pat the Rat; 07-08-2014 at 08:24 AM.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,941
    if you use DOM correctly then your referencing will be...

    document.formName.formElementName.property

    so adding a new onclick would be something like

    document.formName.formElementName.onclick = function(){ /* stuff in here */}

    in one of my scripts I have a loop that grabs the particular element called doc, it then applies an onclick event to it like so...
    Code:
    doc.onclick = function(){ report("Mouse Click in cell : " + this.innerHTML);} // this will be replaced with something useful
    STOP using $ prefix on JavaScript variable names...
    Please remember to wrap any code you have in forum tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  4. #4
    Join Date
    Nov 2009
    Posts
    13
    that seems to work. thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles