I am implementing Google Analytics Goals into my themes. Basically I would like to add an onclick event to the comment submit form. The end result should look like:

 <input type="submit" name="op" id="edit-submit" value="Opslaan"  onclick="javascript: _gaq.push(['_trackPageview', 'social/reactie']);" class="form-submit" />

The problem is that my code also replaces all instances of the single quote with &#039;. I have no idea how to escape these characters. So basically my current implementation looks like (note the instances of &#039; in the onclick event) :

 <input type="submit" name="op" id="edit-submit" value="Opslaan"  onclick="javascript: _gaq.push([&#039;_trackPageview&#039;, &#039;social/reactie&#039;]);"   class="form-submit" />

I use the following code in my template.php file:

  function mytheme_comment_form($form) {
  ........
  $form['comment_filter']['comment']['#title'] = 'Beschrijf jouw ervaringen met deze sauna';
  $form['submit']['#attributes']['onclick'] =  "javascript: _gaq.push(['_trackPageview', 'social/comment']);";
  return drupal_render($form);
  }

How should I adjust this code to have my onclick event shown in the HTML correctly.

Update:
I tried using custom modules with hook_form_alter but got the same problem. I now used the very old fashioned str_replace on the comment wrapper. At least the onclick is now added to the button. If anybody knows a better solution, please post.

Comments

roger.ajith’s picture

Using drupal_add_js(drupal_get_path('module', modulename’).'/my.js');
To import the js file and then call the function in onclick