Problem with for each and Mapbox

Problem with for each and Mapbox

So basically i'm having trouble with an click event firing a popup in Mapbox but the real issue is with
the for each function. I click the third icon in the sequence and it fires the previous two popups, if i click the second one in the sequence it will fire the second and first but not the third.

  1. echo '
  2. var map = new mapboxgl.Map({
  3.    container: "map",
  4.    style: "mapbox://styles/mapbox/streets-v9",
  5.    center:[' . $longitude . ', ' . $latitude . '],
  6.    zoom: 15
  7. });

  8. // add markers to map
  9. geojson.features.forEach(function(marker) {
  10.    // create a DOM element for the marker
  11.    var el = document.createElement("div");
  12.    el.className = "marker";
  13.    

  14.    
  15.    
  16.    el.style.width = marker.properties.iconSize[0] + "px";
  17.    el.style.height = marker.properties.iconSize[1] + "px";
  18.    
  19.    
  20.   
  21.     var popupsmall = new mapboxgl.Popup({
  22.     closeOnClick: false
  23.     })
  24.        .setLngLat(marker.geometry.coordinates)
  25.        //.setHTML(\'<img src="\' + marker.properties.placeImage + \'" style="width:150px; height:100px;padding:0px;"/>\')
  26.         .setHTML("<h4 style=\"color:#6eebdf;margin-top:15px;font-size:24px;\">&pound;" + marker.properties.ppw + "</h4>")
  27.        .addTo(map);
  28. });



  29. geojson.features.forEach(function(marker) {


  30.        $(".mapboxgl-popup").on("click", function(e) {

  31.         var selected = $(e.target).attr("class");
  32.        
  33.         alert(this.id);


  34.         var popup = new mapboxgl.Popup({
  35.     closeOnClick: true
  36.     })
  37.        .setLngLat(marker.geometry.coordinates)
  38.        .setHTML(\'<a href="./property.php?id=\' + marker.properties.propertyid +  \'"><img src="\' + marker.properties.placeImage + \'" style="width:190px; height:100px;padding:0px;"/></a><br><p style="font-family: Hind, sans-serif; font-size:20px;margin-top:3px; padding-left:5px; ">\' + marker.properties.address2 + \'<br><h4 style=\"color:#11EFE0;margin-top:5px;font-size:20px; font-family: Hind, sans-serif; font-weight:600;\">&pound;\' + marker.properties.ppw + \' Per week\')
  39.         //.setHTML("<h4 style=\"color:#11EFE0;margin-top:5px;font-size:20px;\">&pound;" + marker.properties.ppw + "</h4>")
  40.        .addTo(map);
  41.        console.log(marker.properties.address2);
  42.        });



  43.         

  44.       

  45. map.on("click", function(e) {

  46. var popupsmall = new mapboxgl.Popup({
  47.     closeOnClick: true
  48.     })
  49.        .setLngLat(marker.geometry.coordinates)
  50.        //.setHTML(\'<img src="\' + marker.properties.placeImage + \'" style="width:150px; height:100px;padding:0px;"/>\')
  51.         .setHTML("<h4 style=\"color:#6eebdf;margin-top:15px;font-size:24px;\">&pound;" + marker.properties.ppw + "</h4>")
  52.        .addTo(map);

  53.         $(".mapboxgl-popup").on("click", function(e) {
  54.         popupsmall.remove();
  55.          
  56.         var popup = new mapboxgl.Popup({
  57.     closeOnClick: true
  58.     })
  59.        .setLngLat(marker.geometry.coordinates)
  60.        .setHTML(\'<a href="./property.php?id=\' + marker.properties.propertyid +  \'"><img src="\' + marker.properties.placeImage + \'" style="width:190px; height:100px;padding:0px;"/></a><br><p style="font-family: Hind, sans-serif; font-size:20px;margin-top:3px; padding-left:5px; ">\' + marker.properties.address2 + \'<br><h4 style=\"color:#11EFE0;margin-top:5px;font-size:20px; font-family: Hind, sans-serif; font-weight:600;\">&pound;\' + marker.properties.ppw + \' Per week\')
  61.         //.setHTML("<h4 style=\"color:#11EFE0;margin-top:5px;font-size:20px;\">&pound;" + marker.properties.ppw + "</h4>")
  62.        .addTo(map);
  63.        });
  64. });
  65. });
  66.    



  67. </script>';

Not too sure how the formatting looks here but it seems okay in my editor.

Greatly appreciated if any of you could help me.