Search code examples
javascriptpopupleafletscript-tag

Using <script> within a leaflet marker popup


This is going to be a very simple question.

Is there a way of using <script> .... </script> within a Leaflet marker popup ? If not, is there a way around it (ie is there a way to save what the script returns and implement that return in the marker popup) ?


Solution

  • You may be mixing 2 actions: loading the script and calling the rateYo() function

    Loading the script has nothing particular ...

    <script src="https://cdnjs.cloudflare.com/ajax/libs/rateYo/2.3.2/jquery.rateyo.min.js"></script>
    

    Then you must call the function when the popup opens:

    var marker = L.marker([51.5, -0.09]).addTo(mymap)
        .bindPopup("I am a popup.<div id=\"rateYo\"></div>");
    marker.id = something;
    
    marker.on('popupopen', function (e) {
        // the id of the clicked marker is e.target.id
        // retrieve the rating for this id and use it in the rateYo() call
        $("#rateYo").rateYo({
        rating: 3.6,
        starWidth: "15px"
      });
    

    here is an example: https://plnkr.co/8PZIgd2g4Da6AHhWvC2o