Unable to invoke a JavaScript function from an anchor element within innerHTML - the function is undefined

I have a series of collapsible sections that display data from a geoJson file and are dynamically updated as you zoom in and out on a map to show markers within the map's bounds.

When you click on a collapsible section, it expands to reveal details about the specific feature.

I want to include a link or button below the displayed information labeled 'Zoom To Marker', which, when clicked, will zoom the map to the location of the marker.

Despite my efforts, I keep running into issues with the ZoomTo() function being undefined. The ZoomTo() function is situated above the function in the external JavaScript file responsible for creating the list and link.

This problem is really frustrating me because it seems like such a simple task. Although the link appears fine, clicking on it triggers an error message:

Uncaught ReferenceError: ZoomTo is not defined
javascript:ZoomTo(extent);:1

I've attempted various solutions from StackOverflow without success—such as making it global, reordering the functions, using buttons, and appending them. Both functions reside within an initMap function called when the window loads.

The FeatureType function is invoked by event handlers whenever the map changes.

function ZoomTo(extent) {
  //map.getView().fit(extent,{padding: [100, 100, 100, 100],maxZoom:15, duration:500}); 
  alert(extent);
}

function FeatureType(mapfeaturetype, mapExtent) {
  htmlStr = "<div class='accordion' id="
  accordionExample ">";

  // loop through the feature array
  for (var i = 0, ii = mapfeaturetype.length; i < ii; ++i) {
    var featuretemp = mapfeaturetype[i];

    // retrieve geometry for each feature point
    var geometry = featuretemp.getGeometry();
    var extent = geometry.getExtent();
    extent = ol.proj.transformExtent(extent, 'EPSG:3857', 'EPSG:4326');

    // If the feature falls within the map view bounds, display its details in a collapsible section in the side menu
    var inExtent = (ol.extent.containsExtent(mapExtent, extent));
    if (inExtent) {

      htmlStr += "<div class='accordion-item'><div class='accordion-header' id='oneline'>"
      htmlStr += "<span class='image'><img src=" + imgType + "></span><span class='text'>"
      htmlStr += "<a class='btn' data-bs-toggle='collapse' data-bs-target='#collapse" + i + "' href='#collapse" + i + ""
      htmlStr += "aria-expanded='false' aria-controls='collapse" + i + "'>" + featuretemp.get('Name') + "</a></span>"
      htmlStr += "</div><div id='collapse" + i + "' class='accordion-collapse collapse' data-bs-parent='#" + accordionid + "'>"
      htmlStr += "<div class='accordion-body'><h3>" + featuretemp.get('Address') + "</h3><h3>" + featuretemp.get('ContactNo') + "</h3>"
      htmlStr += "<h5><a href=" + featuretemp.get('Website') + " target='_blank'> " + featuretemp.get('Website') + " </a></h5>"
      htmlStr += "<h5>" + featuretemp.get('Email') + "</h5><h5>" + featuretemp.get('Descriptio') + "</h5>"
      htmlStr += "<div id='zoom'><a href='javascript:ZoomTo(extent);'>Zoom to Marker</a></div>"
      htmlStr += "</div></div></div>";

    }; // end if 
  }; // end loop

  htmlStr += "</div>"
  document.getElementById("jsoncontent").innerHTML += htmlStr
}

Answer №1

Utilize a button with an event listener

Your issue can be simplified with the following example:

An uncomplicated HTML file containing a script (utilizing a module).

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="module" src="script.js"></script>
</head>
<body>
    
</body>
</html>

The provided script defines and executes two functions.

function ZoomTo(location) {
    alert(location);
}

function FeatureType() {
    document.body.innerHTML += "<a href='javascript:ZoomTo(extent);'>Zoom to Marker</a>";
}

FeatureType()

The above code replicates the exact error you are encountering.

To resolve it, we suggest employing a more advanced method for generating HTML and managing events.

In this improved version, we create a button that triggers the ZoomTo function via an event handler.

function FeatureType() {
    const button = document.createElement('button');
    button.textContent = 'Zoom to marker';
    document.body.append(button);
    button.addEventListener('click', ev => {
        ZoomTo('test');
    });
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The file selection feature in the browser is malfunctioning when attempting to upload files using either JQuery or JavaScript

I am experiencing an issue where the file select explorer closes after three clicks using jQuery. Below is my code: header.html: $(document).on('click', '.browse', function(){ var file = $(this).parent().parent().parent().find(&ap ...

Angular Express encountering URL mismatch when retrieving files post-refresh

Currently, I am developing a small MEAN stack application. One of the features is that when an image is clicked, it displays a partial containing information about the image. Everything was working fine initially. However, after refreshing the page, Angula ...

The file_get_contents() function encountered an issue as the content type was not specified, leading to it assuming

I am currently working on a project using PHP and JavaScript. I need to call an API from the camera using PHP code. I am using the file_get_contents function and Post request for this purpose. Below is the code snippet: $value = $_POST['TakeNewPic&ap ...

Ways to populate a dropdown menu by choosing a value from a different dropdown menu

Is there a way to create a dropdown list of employees by choosing the department from another dropdown menu in HTML? I need to display a list of employees who belong to the selected department in the second dropdown list. ...

A guide on leveraging console.log in Next.js

I'm having trouble displaying the output of an API call using console.log. The issue is that nothing appears in the console (both in the browser and Visual Studio Code). The only console.log that seems to work is within the RouteStatus function, but i ...

Enhancing Javascript HREF links

Occasionally, I visit URLs that have page numbers indicated in the HREF. As these page numbers change, keeping track of the last one visited becomes a challenge. Cutting and pasting into a text file is a temporary solution; therefore, I intend to replace i ...

Whenever I trim down the images, the captions somehow get lost in the thumbnail

My issue arises when cropping photos, as the captions end up being hidden within the thumbnail images. How can I ensure that the captions are visible and also make all images the same height? I am striving to achieve a layout similar to this: ![][2] ...

Updating materials within the Forge

Currently, we have implemented a system where the client retrieves object states when the page loads, changing the colors of 'pending' objects in the model. We continue to poll for changes to update the coloring - initially coloring pending objec ...

Transferring data from Sequelize mySQL to MS SQL Server

We are currently in the process of migrating our database from mySQL to MS SQL, and we are utilizing Sequelize in NodeJS for our API. One issue arises when using the .findOrCreate method. When I make a request to create a new record that does not exist i ...

Tips for placing a marker at the center of the screen on Google Maps

I am developing a transportation app similar to Uber or Lyft using JavaScript. I am looking to retrieve the map location with the center of the screen, where there is a marker located at y = 0 and x = 0. Similar to the image below: The user should be abl ...

Encountering difficulties in sending a JavaScript array through jQuery ajax request

I'm feeling hesitant to ask this, but I can't figure out why my code isn't working. Here's what I have: <script> var formArray = new Array(); formArray['start'] = "one"; formArray['stopat'] = "two"; formArray ...

Exploring nested data structures in Vue.js

Within my Vue-app, I am attempting to iterate through an array of data that contains multiple checkboxes. filters: [ { name: "Sales & marketing", open: false, items: [ { employees_cvr: { plac ...

Incorporating additional options onto the menu

I recently designed a menu on https://codepen.io/ettrics/pen/ZYqKGb with 5 unique menu titles. However, I now have the need to add a sixth title to the menu. After attempting to do so by adding "strip6" in my CSS, the menu ended up malfunctioning and looki ...

Submitting a form using jquery

I am working on a project that involves using a jquery fancyzoom box. Within this box, there is a contact form that should send an email upon submission. However, I am encountering issues with calling the form submit function due to the fancyzoom feature. ...

Ways to Customize the new Date() Function to Display Only Specific Fields

I am attempting to format my this.state.date to appear as DD/MM/YYYY. To achieve this, I utilized the library found at https://github.com/mmazzarolo/react-native-modal-datetime-picker Currently, I can select my desired date, however, when I assign it to t ...

Understanding the Camera's View in Three.js

I am currently developing a game using html5 and THREE.js, where I have implemented a camera that rotates with the euler order of 'YXZ'. This setup allows the camera to rotate up, down, left, and right – simulating a first person view experien ...

Leveraging the File System functions of node.js within a web browser

Need help creating a function to delete files from the server? Want to reset settings like db files to their default state? When running your server, do you use the command: node server.js Familiar with node.js's File System but unsure how to utiliz ...

The most effective way to code overlapping html buttons

Can you help me figure out how to make clickable areas on my HTML5 web page without them overlapping? Here's what I'm trying to do: I want the red, blue, and green areas to be clickable links, but not overlap. For example, when clicking on the f ...

Utilize video frames to create a dynamic canvas background in JavaScript

I want to use the first frame of a video as the background for a canvas, but I've been struggling with it. I can set a background and extract a frame from a video, but I can't seem to make that frame the background. It seems like I need a URL for ...

How can I cancel or reset a timeInterval in AngularJS?

In my project demo, I have implemented a feature that fetches data from the server at regular intervals using $interval. Now, I am looking for a way to stop or cancel this process. Can you guide me on how to achieve this? And if I need to restart the proce ...