...

View Full Version : Combining two onclick js functions



bu06nne
11-16-2012, 11:13 AM
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,

Philip M
11-16-2012, 11:18 AM
<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 - )

bu06nne
11-16-2012, 03:26 PM
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

niralsoni
11-16-2012, 03:42 PM
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 -



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

felgall
11-16-2012, 10:04 PM
You could swap the function calls around the other way assuming they are independent of one another.


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

bu06nne
11-20-2012, 10:09 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum