Incorporate the ability to display a shape on a map when hovering over a table element, without the need to manually code a JavaScript function for every instance

I came across a script online that allows me to hover over text and have a shape appear on an imagemap. It's functional, but only works for a single instance. Is there a way to implement a JavaScript that handles individual instances so I don't have to write separate scripts for each one? My texts are organized in a table.

Here is the current JavaScript code:

<HEAD>.....
 <script type="text/javascript">$(function() {
    $('.map').maphilight();
    $('#squidheadlink').mouseover(function(e) {
        $('#squidhead').mouseover();
    }).mouseout(function(e) {
        $('#squidhead').mouseout();
    }).click(function(e) { e.preventDefault(); });
});
</script>


<BODY>......
<map.....
<area id="squidhead" href="#" shape="rect" coords="680,55,730,110"  alt="Octoface"     data maphilight='{"stroke":false,"fillColor":"ff0000","fillOpacity":0.6}'>
<area id="xxx" href="#" shape="circle" coords="298,552,10" alt="Octoface"  data-maphilight='{"stroke":false,"fillColor":"ff0000","fillOpacity":0.6}'>
</map>
</div>
<DIV ID="header" STYLE="position:absolute; top:450px; left:20px; width:1490px;  height:225px;">
<table class="tftable" border="0">
<tr><th>Header 1</th><th>Header 2</th><th>Header 3</th><th>Header 4</th><th>Header  5</th><th>Header 6</th><th>Header 7</th><th>Header 8</th><th>Header 9</th><th>Header 10</th></tr>
<tr>
<td><a id="squidheadlink" href="#">gyrex</a></td>
<td><a id="xxxlink" href="#">xxx</a></td>

In the above example, there is a second item to highlight, which references the following code duplicating the "squidhead" section with a different name:

<script type="text/javascript">
$(function() {
    $('.map').maphilight();
    $('#xxxlink').mouseover(function(e) {
        $('#xxx').mouseover();
    }).mouseout(function(e) {
        $('#xxx').mouseout();
    }).click(function(e) { e.preventDefault(); });
});</script>

My question is, can the JavaScript be modified to apply to the entire array of items in the table used to highlight different areas on the map/image?

The "id" items reference their respective JS scripts, requiring separate scripts for each instance, leading to potential repetition...

The resolved script, thanks to @levi:

After extensive reworking of the code, it finally works...the previous advice was insightful...with numerous variables at play, the final script:
$(function() { $('.map').maphilight(); $('.tftable td a').each(function () { var areaId = '#' + this.id.replace('link', ''); $(this).mouseover(function (e) { $(areaId).mouseover(); }).mouseout(function (e) { $(areaId).mouseout(); }).click(function (e) { e.preventDefault(); }); }); });

Answer №1

To efficiently apply the code to numerous instances, utilize a loop:

 $('.tftable td a').each(function () {
    var elementId = '#' + this.id.replace('link', '');
    $(this).mouseover(function (e) {
        $(elementId).mouseover();
    }).mouseout(function (e) {
        $(elementId).mouseout();
    }).click(function (e) {
        e.preventDefault();
    });
});

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

Building a Sharepoint application with Angular 2 using the CLI and Webpack

Trying to deploy an Angular 2 CLI App to a SharePoint 2013 site has been quite challenging. The app works fine when embedded via a Content Editor Webpart, but the console throws an exception: zone.js:158 Uncaught Error: Sys.ParameterCountException: Parame ...

Error: The value of _This.props is not defined

Hey there, I am new to working with React Native and could really use some assistance with this issue: TypeError: _props.goToScreen is not a function. Any help would be greatly appreciated, thank you in advance! In my Profile.js file, I am trying to click ...

Activate the class using Vue with the v-for directive

I'm curious about the v-for functionality. Why is it necessary to use this.activeClass = {...this.activeClass} to update the component? I noticed that the component does not update after this line of code. if (this.activeClass[index]) { ...

An error occurs when attempting to redirect with getServerSideProps

When I am logged in, I want to redirect to the /chat page using auth0 for authentication. The error seems to be related to returning an empty string for props, but it does not impact the website as redirection works correctly. The main issue here is the in ...

Exploring Vue Slots: A guide to parsing and rendering slot components

Currently facing a challenge while developing a web page using Vue, specifically with parsing and rendering the child components inside the <slot>. I need to extract the slot content, convert it into an array of components, and display these compo ...

Chrome fails to read font family correctly

I created a jsfiddle that demonstrates an interesting behavior with setting and reading the font-family using jQuery. When I set the font-family to "Arial . . ." it reads back okay, enclosed in a single set of double quotes. However, when I set the font-fa ...

What is the best way to execute an inline function two times in a row

I'm currently utilizing Gametime.js to create a real-time world chat feature. All messages are kept in a database for storage. Interestingly, PubNub, which is used by Gametime.js, seems to require messages to be sent twice for them to actually go th ...

The time.js library is providing inaccurate second values for the given date and time

Utilizing the moment.js library along with the Timezone and BusinessDays extensions in Vue.js, I am developing a datetime format for storing in a MySQL database. Here is the code snippet: import moment from 'moment-timezone' ...

"Incorporating Node.js (crypto) to create a 32-byte SHA256 hash can prevent the occurrence of a bad key size error triggered by tweetnacl.js. Learn how to efficiently

Utilizing the crypto module within node.js, I am creating a SHA256 hash as shown below: const key = crypto.createHmac('sha256', data).digest('hex'); However, when passing this key to tweetnacl's secretbox, an error of bad key siz ...

Mastering callback functions within AngularJS animations

As I delve into AngularJS animations, I am currently working on a slide-show animation: app.animation('slide-show', function () { return { setup: function (element) { }, start: function (element, done) { e ...

JQuery draggable and JavaScript functionality become disabled following an AJAX request

I have a click event declared as follows: $('body').on('click', 'input#searchVariables', function () { var datasetId = $('.TSDatasetID').val(); var term = $('#searchTextbox').val(); ...

Tips for refreshing a table component after receiving a notification from a WebSocket in React JS

Currently, I am utilizing React table to load a page that shows a table with data fetched from an API. Additionally, I am listening on a web socket and whenever there is new data sent over the web socket, a console message is printed. My goal now is to a ...

Disabling ESLint errors is not possible within a React environment

I encountered an eslint error while attempting to commit the branch 147:14 error Expected an assignment or function call and instead saw an expression @typescript-eslint/no-unused-expressions I'm struggling to identify the issue in the code, even ...

Troubleshooting Angular: Unidentified property 'clear' error in testing

I've implemented a component as shown below: <select #tabSelect (change)="tabLoad($event.target.value)" class="mr-2"> <option value="tab1">First tab</option> <op ...

Issue with Webpack: error message "Cannot read property 'readFile' of undefined" is causing no output files to be generated

When utilizing version webpack > 5, the configuration for my appDevMiddleware.js is as follows: const path = require('path'); const webpack = require('webpack'); const webpackDevMiddleware = require('webpack-dev-middleware' ...

The inner workings of v8's fast object storage method

After exploring the answer to whether v8 rehashes when an object grows, I am intrigued by how v8 manages to store "fast" objects. According to the response: Fast mode for property access is significantly faster, but it requires knowledge of the object&ap ...

What is the process of defining a route for a JSON response in an Express application?

I've been following an Angular tutorial on handling form submissions with promises, which I found here. My server is running on Node and I'm using Express to manage routes. However, when I submit the form and reach the line var $promise = $http. ...

Coding with a combination of JavaScript, AngularJS, and manipulating brackets

I am currently performing the following action: myArray.push(pageCount); After that, I end up with something like this: $scope.myArray = Pages.getAllPageCount(); Finally, when I utilize AngularJS to display it in my .html file. {{myArray}} If there i ...

Alignment of Material-UI dialogue buttons on the left side

I have a Dialog containing three buttons as shown below: https://i.stack.imgur.com/T6o35.png Here is the JSX code: <DialogActions classes={{ root: this.props.classes.dialogActionsRoot }} > <Button classes={{ root: this.props ...

Creating dropdown menus dynamically and populating them based on the selection made in one dropdown menu to determine the options available

Looking to enhance the filtering options in my ngGrid, I stumbled upon the concept of Filtering in Ignite UI grid and was impressed by its functionality. I am now attempting to implement a similar feature in AngularJS. Breaking down the task into 4 compon ...