Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Combining two onclick js functions

    Hi folks,

    I need to add an onclick function to a submit button for a form that was built by a developer.

    How would I do this, if the submit button already has an onclick function already there?

    <div id="formbuttons"><input class="buttonsubmit" id="btnsubmit" name="btnsubmit" onclick="return formvalidate()" type="submit" value="Send"/> </div>
    <input type="hidden" name="subaction" value="form" /> <input type="hidden" name="formId" value="OnlineFormHandler"/></form>


    I need to add the following to the submit button:

    onclick="wrs_trackclick('DCS.dcsuri=formsubmit', 'WT.ti=form submit','WT.dl=24')

    Many thanks,

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,906
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <div id="formbuttons"><input class="buttonsubmit" id="btnsubmit" name="btnsubmit" onclick="return formvalidate(); wrs_trackclick('DCS.dcsuri=formsubmit', 'WT.ti=form submit','WT.dl=24')" type="submit" value="Send"/> </div>
    "Most of the time I don't have much fun. The rest of the time I don't have any fun at all."- oody Allen - US movie actor, comedian, & director (1935 - )

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your swift response...

    Unfortunately that code doesn't seem to work.

    The form validation part works, but the Webtrends analytics code part doesn't seem to be firing off...

    Any ideas what it could be?

    Many thanks,

    Nick

  • #4
    Regular Coder
    Join Date
    Mar 2008
    Location
    London
    Posts
    152
    Thanks
    4
    Thanked 42 Times in 42 Posts
    It didn't worked because the onclick code is prefixed with "return" keyword, which will return after completing the first function, and won't execute the second function.

    if your validation function is returning true or false then try this out -

    Code:
    <div id="formbuttons"><input class="buttonsubmit" id="btnsubmit" name="btnsubmit" onclick="if(formvalidate()) {wrs_trackclick('DCS.dcsuri=formsubmit', 'WT.ti=form submit','WT.dl=24'); return true;} return false;" type="submit" value="Send"/> </div>

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,603
    Thanks
    0
    Thanked 645 Times in 635 Posts
    You could swap the function calls around the other way assuming they are independent of one another.

    Code:
    <div id="formbuttons"><input class="buttonsubmit" id="btnsubmit" name="btnsubmit" onclick="wrs_trackclick('DCS.dcsuri=formsubmit', 'WT.ti=form submit','WT.dl=24'); return formvalidate(); " type="submit" value="Send"/> </div>
    This is an example of why it is better to use event listeners in JavaScript rather than event handlers - you would have been able to add a second submit listener without needing to update any of the already existing code.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #6
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for both your replies.

    Unfortunately neither of them worked, the form validates, but the webtrends javascript function doesn't fire off. Any ideas what I should I do now?

    Cheers,
    Nick


  •  

    Posting Permissions

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