Shaun Brown
Shaun Brown

Reputation: 123

Javascript store locator, Uncaught ReferenceError: ... is not defined

I have set an onclick with a variable on a link that calls the calcRoute function for Google maps, and everytime I click the link it says the following Uncaught ReferenceError: NE461UL is not defined The parameter is a postcode by the way.

I have been trying for a while and I can't figure out why it is showing an error.

I have a jquery file with the following line

var distLink = "<a onclick=\"calcRoute(" + postcode +  "); return false;\" datalat=" + lat + " datalng=" + lng + " id=\"get-directions\" class=\"directions" + letter +"\">Directions<\/a>";

The calcRoute is in my header

function calcRoute(postcode) {
  console.log(marker);
  var start = document.getElementById("address").value;
  var end = document.getElementById("get-directions").name;
 $('get-directions').click(function(){
    console.log('click!');

});
  var request = {
    origin:start,
    destination:end,
    travelMode: google.maps.TravelMode.DRIVING
  };
  console.log(JSON.stringify(request)); 
   directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}

Upvotes: 3

Views: 1692

Answers (1)

Marcelo
Marcelo

Reputation: 9407

postcode must be passed as a string (extra pair of quotation marks around it):

distLink = "<a onclick=\"calcRoute('" + postcode +  "'); return false;\" datalat=" + lat + " datalng=" + lng + " id=\"get-directions\" class=\"directions" + letter +"\">Directions<\/a>";

Upvotes: 2

Related Questions