0

My JavaScript is like this:

var contentItem = '<a onclick="ga('send', 'event', 'find-a-doctor', 'appointment', 'Appointment from Home');" class="waves-effect waves-dark">Make an Appointment</a>';

Quotation marks above seems wrong. How do I fix this?

freedomn-m
  • 27,664
  • 8
  • 35
  • 57
moses toh
  • 12,344
  • 71
  • 243
  • 443

3 Answers3

1

You could use `` (backquotes or backticks), e.g.:

var contentItem = `<a onclick="ga('send', 'event', 'find-a-doctor', 'appointment', 'Appointment from Home');" class="waves-effect waves-dark">Make an Appointment</a>`;
Yashwardhan Pauranik
  • 5,370
  • 5
  • 42
  • 65
Alessio Cantarella
  • 5,077
  • 3
  • 27
  • 34
1

Inline JS will cause all kinds of issues. Instead you could adopt a modern approach and use an event listener:

// Create your item and add it to the page
var contentItem = '<a class="waves-effect waves-dark">Make an Appointment</a>';
document.body.insertAdjacentHTML('beforeend', contentItem);

// Cache the element you added and add an event listener to it
// that calls handleClick when clicked
const wavesEffect = document.querySelector('.waves-effect');
wavesEffect.addEventListener('click', handleClick, false);

// Now your call to `ga` is much cleaner
function handleClick(e) {
  console.log('send', 'event', 'find-a-doctor', 'appointment', 'Appointment from Home');
  // ga('send', 'event', 'find-a-doctor', 'appointment', 'Appointment from Home');
}
Andy
  • 61,948
  • 13
  • 68
  • 95
0

You can try following code.

var contentItem = '<a onclick="ga(\'send\', \'event\', \'find-a-doctor\', \'appointment\', \'Appointment from Home\');" class="waves-effect waves-dark">Make an Appointment</a>';
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
James
  • 9
  • 1