Using Leaflet: How do I get it to display one popup based on a condition. See below.

    var layerOrder=new Array();
    function pop_thunder(feature, layer) {                  

    if (feature.properties.CODE = 0) {

    var popupContent = feature.properties.html1_exp;
    else {
    var popupContent = feature.properties.html_exp;

You can use the onEachFeature option of the GeoJSON layer to append the popups to each feature. And you can switch the popup contents inside of the onEachFeature function.

I made an example on jsfiddle that you can use to see how it works.

As you can see, the example displays a GeoJSON Layer with three markers on the map, two makers are cities with code of 0, and they use the texts from the "description" field in the popup window; and the third marker is a national park with code of 1, and it uses the the texts from the "description1" field in the popoup window.

|improve this answer|||||
  • Would I need to code each feature in the collection? I have hundreds of features. I also do not have markers right now, although I am considering changing it so that only features with the code of 1 have a marker on them. Thanks for the sample code this helps give me ideas on ways to proceed for a solution. @vanni-zhang – Tami Aug 5 '15 at 13:53

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.