7

I'm working with the Leaflet JavaScript library. There should be an option to click on my map and view a popup. I already have a code for that:

var popup = L.popup();

function onMapClick(e) {
popup
    .setLatLng(e.latlng)
    .setContent(e.latlng.toString() + "<br><b>Visit Google</b>")
    .openOn(map);
}

map.on('click', onMapClick);

This code is working, but my question is: how can i put an link to that popup? For example there should be a link called "Visit Google". When you click on that link it should open www.google.com (surprise). I think it should be in the .setContent(e.latlng.toString()) tag, because I can add there a text like Visit Google (as you can see).

Any ideas ?

17

You are providing html in setContent()

var popup = L.popup();

function onMapClick(e) {
popup
    .setLatLng(e.latlng)
    .setContent(e.latlng.toString() + '<a href="http://www.google.com">Visit Google</a>"')
    .openOn(map);
}

map.on('click', onMapClick);
  • 1
    Thanks a lot! It worked :) I would vote your comment up, but my reputation score is too low :D – Jandroide Aug 22 '14 at 12:09
  • Now I was able to vote your comment up ;) – Jandroide Aug 27 '14 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.