Carla Dessi
Carla Dessi

Reputation: 9666

Javascript function won't pass parameter

I have the following code:

function createMarker(latlng, name, address) {
  var html = "<b>" + name + "</b> <br/>" + address + "<br/> <a onclick='moreInfo(name)'>Learn More</a>";
  var marker = new google.maps.Marker({
    map: map,
    position: latlng
  });
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
  markers.push(marker);
}

function moreInfo(name){

    $('#dcFinder').hide();
    $('#moreInfo').show();
    $('#name').html(name);

}

Which refers to the google map store locator I'm working on. I'm trying to make a link to the moreInfo function, passing through the name used in the first function.

When the link is clicked, the appropriate divs are shown and hidden, but the name is not showing.

Upvotes: 0

Views: 178

Answers (2)

Anton
Anton

Reputation: 32581

You need to concatenate the string properly and escape quotation marks for the function variable:

var html = '<b>' + name + '</b> <br/>' + address + '<br/> <a onclick="moreInfo(\'' + name + '\')">Learn More</a>';

Upvotes: 2

Pete
Pete

Reputation: 58412

You need to pass through the variable in your html so try this:

 var html = "<b>" + name + "</b> <br/>" + address + "<br/> <a onclick=\"moreInfo('" + name + "')\">Learn More</a>";

Upvotes: 3

Related Questions