jQuery Hover Event Handling

I'm encountering an issue with syncing hover effects in two separate tables. Oddly, the first part of the function works fine on its own. However, when I add the second part, it breaks the first part without any errors.

I refrained from including it in the jsfiddle because the problem lies within the code itself, not its visual representation.

$(function(){
    //first part
        var trsCont = $('#conteudo table tr');
        for (i = 0; i < trsCont.length; i++) {
            trsCont.eq(i).hover(
                function () {
                    $('#coluna_fixa table tr').eq(i-1).toggleClass("hovered");
                    }
                );
            }
        //second part   
        var trsCol = $('#coluna_fixa table tr');
        for (i = 0; i < trsCol.length; i++) {
            trsCol.eq(i).hover(
                function () {
                    $('#conteudo table tr').eq(i+1).toggleClass("hovered");
                }
            );
        }
});

I am aware that I am making a mistake somewhere. Can anyone help me pinpoint it?

Thank you for reading up to this point.

Answer №1

A best practice is to avoid defining event handlers inside loops. It's recommended to make your hover functions more generic, as demonstrated below:

//first part
       $('#table1 tr').hover(
           function () {
               var index = $(this).index();
               $("#table2 tr:eq(" + (index - 1) + ")").toggleClass("hovered");
           }
       );
        //second part   
        $('#table2 tr').hover(
           function () {
               var index = $(this).index();
               $("#table1 tr:eq(" + (index + 1) + ")").toggleClass("hovered");
           }
       );

For a practical example, refer to this JSFiddle link: http://jsfiddle.net/cAEWR/2/

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

What are the methods for differentiating between a deliberate user click and a click triggered by JavaScript?

Social media platforms like Facebook and Twitter offer buttons such as Like and Follow to allow users to easily engage with content. For example, on Mashable.com, there is a Follow button that, when clicked, automatically makes the user follow Mashable&ap ...

Troubleshooting issues with static serving in Express.js

I'm facing an issue while trying to apply a bootstrap theme to a file created using Jade. The error message indicates that it cannot GET the file. Main JavaScript code snippet: const express = require('express'); const app = express(); ap ...

Steps to link a specific section of a Google pie chart:

I have a question that may be a little confusing, so let me explain. I recently used Google Charts to create a simple pie chart and it worked perfectly. Now, I am trying to figure out how to link each section/part of the pie chart to display a jQuery moda ...

What is the best way to transform a single quote within an element of an array, which is part of an object, into a double quote without having to generate a new array

I've been advised to use a specific code for one of the problems I tackled some time ago. I'm attempting to figure it out but so far, I haven't made any progress. The challenge is to accomplish this task using replace(), map() and more withi ...

The v-model for a particular field is not reflecting real-time updates like the other fields with the same datatype. I'm trying to figure out what could be causing this issue

In my specific model, there are various values that can be updated through a form on the webpage. These values include "Quantity", "Rate", "Amount", "Net rate", and more. The problem I am facing is with binding these values with my inputs using v-model. E ...

css How to target a specific row and column within a table using css selector in Selenium WebDriver

When attempting to locate css=table#Salarytable.dataTable.3.4 in Selenium IDE, the cell highlights correctly. However, when using this locator in my Selenium code... String salary=driver.findElement(By.cssSelector("table#Salarytable.dataTable.3.4")).get ...

Can you identify the origin of the inclusion of a .php file?

Is there a way to automatically determine if a PHP script is being accessed directly as an HTML page, used as a JavaScript file, or utilized as a CSS Stylesheet? I'm looking for a solution that doesn't involve GET variables or setting a flag wit ...

Can someone explain why my search input's sync function is being triggered twice?

Within my CodePen project at https://codepen.io/aaronk488/pen/MWbKNOq?editors=1011, I am facing an issue where my sync function search is being called twice without a clear reason. Even though I managed to resolve this by adding a conditional statement in ...

Guide on sending several HTTP requests from a Node.js server with a shared callback function

Is there a way to efficiently make multiple HTTP calls in a Node.js server with a shared callback function? Are there any modules or libraries that can help with this? ...

Transfer information between two clients using AJAX on the same localhost

I'm looking to create a connect four game with the capability of playing online against another player connected to my localhost (within the same WiFi network). While it's possible to play the game on two separate computers, they currently can&ap ...

Personalizing the dimensions of audio.js

I recently integrated the audio.js plugin into my website. I've added the necessary code to the header.php file located in the includes folder. <script src="audio/audiojs/audio.min.js"></script> While the player appears correctly on othe ...

Angular 2 - Error: Regular expression missing forward slash syntax

Recently, I began working on an Angular 2 tutorial app using this repository. While I can successfully launch the app and display static content, I am facing challenges with rendering dynamic content from the component. I have a feeling that the error migh ...

Identifying Internet Explorer version through CSS functionality and feature detection

As of IE10, browser detection tags are no longer supported for identifying a browser. In order to detect IE10, I am utilizing JavaScript and a technique that tests for certain ms prefixed styles like msTouchAction and msWrapFlow. I would like to do the s ...

Why does the map function in JavaScript not allow for a function argument?

I encountered an issue while calling a function and passing an array of objects as the first argument, with the second argument being an object property of the first argument. Strangely, the map function was not accepting the second argument property. He ...

In certain browsers, the link changes color to white

As I assist my brother in setting up a website for his business, we encountered an issue. In some browsers, hyperlinks appear as white and therefore invisible. Interestingly, the hyperlink is white in Firefox, but not when browsing in incognito mode, see t ...

Error code (6) occurred during the upload process on the Google Drive website, resulting in

I'm currently working on building a website that allows users to upload files directly to Google Drive from the website itself, but I've encountered numerous challenges in the process. Here's the code snippet I've been using: <!DOCT ...

Styling a disabled button in ASP.NET using CSS

I've encountered an issue with my asp.net button that is disabled based on specific c# code conditions. The button, known as btnPrevious, is assigned the css class below: .btnBlue{ background: rgb(40, 108, 244); /* Old browsers */ background:linear-g ...

The differences between getElementById and getElementByClassName

When using both getElementById and getElementByClassName, I have noticed that getElementById sometimes returns null while getElementByClassName works without any issues. I wonder what might be causing this discrepancy. It is my understanding that getElemen ...

What is the best way to eliminate the ' from every element in an array in this situation?

My form has hidden fields with values enclosed in single quotes like 'enable'. Using jQuery, I extract these values and push them into an array. This array is then sent to a PHP file using JavaScript through the url attribute of the jQuery AJAX m ...

What is the best way to change the height of cells within a grid layout?

Enhancing the buttons with a touch of depth using box-shadow resulted in an unexpected outcome - the shadows bleeding through the gaps and causing uneven spacing. https://i.sstatic.net/LYssE.png In an attempt to rectify this issue, I decided to tweak the ...