Uncaught ReferenceError: onchange function is not defined (wordpress)

admin

Administrator
Staff member
I am trying to insert a dropdown list of layers to display the layer selected in a map in wordpress using Openlayers tool. But i get this error when i tried to choose from the drpdown list :

Code:
Uncaught ReferenceError: update is not defined
    at HTMLSelectElement.onchange ((index):226)

My script :

Code:
var geojsonFormat = new ol.format.GeoJSON();

var vectorSource = new ol.source.Vector({
  loader: function(extent, resolution, projection) {
    var url = "http://localhost:8080/geoserver/opengeo/ows?service=WFS&" +
        "version=2.0.0&request=GetFeature&typename=opengeo:arbousiers0&CQL_FILTER=code_espec='Arteherb'"+
        "&outputFormat=text/javascript&format_options=callback:loadFeatures"+
        "&srsname=EPSG:3857";

    $.ajax({url: url, dataType: 'jsonp', jsonp: false});
  },
  strategy: ol.loadingstrategy.tile(ol.tilegrid.createXYZ({
    maxZoom: 19

  }))

});
window.loadFeatures = function(response) {
  vectorSource.addFeatures(geojsonFormat.readFeatures(response));
};

function update() {
  vectorSource.clear(true);
};

    function getRandomColor() {
        var letters = '0123456789ABCDEF';
        var color = '#';
        for (var i = 0; i < 6; i++ ) {
            color += letters[Math.floor(Math.random() * 16)];
        }
        return color;
    }

var map = new ol.Map({
  layers: [
  new ol.layer.Vector({
  source: vectorSource,
  style: new ol.style.Style({
    stroke: new ol.style.Stroke({
      color: 'rgba(0, 0, 255, 0.0)',
      width: 0.3
    }),
     fill : new ol.style.Fill({
   color: getRandomColor()
  })
  })
})
  ],
  target: document.getElementById('mapid'),
  view: new ol.View({
    center: [-1095791.453557, 3422374.879112],
    maxZoom: 19,
    zoom: 5
  })
});

my dropdownlist code :

Code:
<select class='Nom' name="pam" id="pam" onchange="update()">
<option value="">--- Select Layer ---</option>
[insert_php]
$conn_string = "host=localhost port=5432 dbname=postgres user=postgres password=";
$conn = pg_connect($conn_string);


 $sql = pg_query($conn, "SELECT * FROM especes");

while($ligne_liste=pg_fetch_array($sql)) {
    echo '<option value="'.$ligne_liste['nom'].'">'.$ligne_liste['nom']."</option>\n";
}
echo '</select>';
[/insert_php]

and then i followed the answer here to call the onchage event in
Code:
functions.php
:
<a href="https://wordpress.stackexchange.com/questions/248026/add-onchange-to-select-in-a-wp-form">https://wordpress.stackexchange.com/questions/248026/add-onchange-to-select-in-a-wp-form</a>

Code:
function addAttributeToDropdown($html_content){
  $html_content = str_replace('&lt;select','&lt;select onchange="update()"',$html_content);
  return $html_content; 
}

add_filter('wp_dropdown_cats','addAttributeToDropdown');