Is there a way to change the text color of a table when hovering over an image using Javascript?

UPDATE: I believe I have solved the issue! However, if anyone has suggestions on a better way to achieve this, I am open to learning. I'm still fairly new to Javascript!

I have an image and a table containing text. My goal is to change the color of SOME of the text in the table when I hover over the image. The current code works well for a SINGLE sticker, but when I attempt to use a class (e.g. class="sticker3") and .getElementsByClassName("sticker3"), it fails to work.

<span id="sticker3" style="font-size: 30px; margin: 0px; line-height: 0px; color: #000000;">✿</span>
<script>
        function upDate(){
            document.getElementById('sticker3').style.color = "#FF0000";
    }
    
        function unDo(){
            X = document.getElementById('sticker3');
            X.style.color = "";
            document.getElementById('sticker3').style.color = "FFFFFF";
    }
    </script>

When I tried to adjust the code as shown below, it no longer functions.

<span class="sticker3" style="font-size: 30px; margin: 0px; line-height: 0px; color: #000000;">✿</span>
<script>
        function upDate(){
            document.getElementsByClassName('sticker3').style.color = "#FF0000";
    }
    
        function unDo(){
            X = document.getElementsByClassName('sticker3');
            X.style.color = "";
            document.getElementsByClassName('sticker3').style.color = "FFFFFF";
    }
    </script>

I have included an image for better understanding of what I am attempting to achieve. As depicted, only one flower turns red upon hovering over image 1, while I wish for all the flowers in that column to turn red. VIEW IMAGE

Code Snippet

function upDate() {
  document.getElementsByClassName('sticker3').style.color = "#FF0000";
}

function unDo() {
  X = document.getElementsByClassName('sticker3');
  X.style.color = "";
  document.getElementsByClassName('sticker3').style.color = "FFFFFF";
}
body {
  font-family: sans-serif;
}

table {
  border-collapse: collapse;
}

td,
th {
  border: thin solid lightgray;
  padding: 0.2rem;
}

.sticker3:after {
  content: "✿";
  font-size: 20px;
  margin: 0px;
  line-height: 0px;
  color: #000000;
}

.hoverbox {
  width: 10rem;
  display: inline-block;
  border: thin solid gray;
  background-color: lightyellow;
  margin: 0.5rem;
  padding: 0.5rem;
}
<table id="table1">
  <thead>
    <tr>
      <th>Name</th>
      <th>Fig 1</th>
      <th>Fig 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td></td>
      <td><span class="sticker3"></span></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td><span class="sticker3"></span></td>
    </tr>
    <tr>
      <td></td>
      <td><span class="sticker3"></span></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td><span class="sticker3"></span></td>
    </tr>
    <tr>
      <td></td>
      <td><span class="sticker3"></span></td>
      <td></td>
    </tr>
    <tr>
      <td></td>
      <td></td>
      <td><span class="sticker3"></span></td>
    </tr>
  </tbody>

</table>

<div id="hover1" class="hoverbox">Fig 1 Hover</div>
<div id="hover2" class="hoverbox">Fig 2 Hover</div>

Answer №1

You're not accessing the collection that the method returns...

perhaps consider using:

document.getElementsByClassName('sticker3')[0];

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

Guide for using express.js

Similar to how you can use to view the source code of all classes in Java, is there a resource available to view the source code of the express module? ...

Can you explain the definition of "mount" in the context of Express?

Currently diving into the world of Express.js, I stumbled upon this definition: "An Express router offers a limited set of Express methods. To initialize one, we call the .Router() method on the main Express import. To utilize a router, we mount it a ...

dismiss data and button for closing notification bar

I am currently using a notification bar at the top of my website, instead of a modal. I am wondering if it is possible to click the X (close-button) and have the notification bar disappear. I attempted to use data-dismiss but it did not work. Also, how can ...

Idea fails to detect imports

I have been attempting to use Angular2 in IntelliJ IDEA IDE. Although my code is valid (I have tried compiling and executing it), the IDE keeps showing me this error: https://i.stack.imgur.com/w6wIj.jpg Is there a way to configure IntelliJ IDEA to hide t ...

Can you modify the color name's hue in an mplstyle document?

When working with matplotlib plots, I often find myself needing to assign specific colors. While I can change default colors in the mplstyle file, there are times when I want to use my own predefined colors. # Define custom colors in mplstyle file axes.pro ...

HTML: Attempting to extract the inner div from the outer div and relocate it outside of the outer div

I am attempting to extract an inner div from an outer div and relocate it outside of the outer div. https://i.stack.imgur.com/NSS7B.png <div class="vc_gitem-zone-mini"> <div class="vc_gitem_row.............."></div> <div> I Would ...

Using knockout to data bind a function to an onclick event that takes in multiple parameters

I've scoured the internet and experimented with various methods, but I'm encountering an issue where the click function intermittently fails to fire. Below is my HTML code snippet: <input type="radio" data-bind="checked:a, checkedValue: 0 ...

I'm having trouble modifying the backdrop to 'true' or removing it after setting it to 'static' in Bootstrap. Can anyone help me troubleshoot this issue?

I have been encountering an issue with changing the backdrop setting from 'static' to 'true' in Bootstrap modal. Here is the code I am using: $('#modal').modal({backdrop: 'static', keyboard: false, show: true}); ...

exploring numerous boxes in an engaging and educational tutorial

Explaining this in the title was a bit tricky, but what I want to create is an interactive tutorial. In this tutorial, the user will click on an area or product they want to clean or use for cleaning. After clicking, another box with relevant information w ...

Basic Block - Three.JS

My goal was to create a simple rotating 3D cube using the Three.js library for WebGL. Despite following numerous tutorials, I can't figure out why my code only displays a black screen with no geometry. //Setting window dimensions var width = windo ...

Storing user input from Vue.js in a JavaScript array for future use

Utilizing vue.js <template> <input id="email" v-model="email" type="text" placeholder="Email"> <input id="name" v-model="name" type="text" placeholder=" ...

css issue with opacity transition not functioning properly

My website is designed to display images in a table, but some of them are Stock Photos, so I need to indicate the source. I want the source to appear when the user hovers over the picture. For this purpose, I have assigned the source tag to a class (.PicSo ...

Remove duplicate JSON records in JavaScript by comparing and filtering based on identical attributes

Looking to remove duplicates from a JSON object [{id:1,name:a, cat:1},{id:1, name:a, cat:2},{id:2, name:b, cat:8}] I want to keep only the first occurrence of each duplicated id [{id:1,name:a, cat:1},{id:2, name:b, cat:8}] ...

Issue with PLUploader in ASP.Net/JQuery: "Add Files" button not functioning post image upload操作

Hello, I've been utilizing the PLUploader controller for ASP.NET in C#. Initially, it works as intended, but after uploading files, the "add files" button becomes disabled or stops working, preventing me from adding more images. Can anyone offer assi ...

Axios error in Express middleware - unable to send headers once they have been processed

I can't seem to figure out why my code is not running correctly. While using Axios in my middleware, I am encountering this error message: Error: Can't set headers after they are sent. This is the snippet of my code (utilizing Lodash forEach): ...

What is the best way to insert an HTML data attribute string into a SCSS mixin using attr()?

I'm currently working on setting up a color scheme in SCSS that allows me to use the following HTML structure: <div class="swatch" data-bg="green">...</div> In my SCSS file, I have defined a mixin like this: @function color($key: ' ...

What is the best way to locate and list all video links, and include options for "Play Video" and "Download Video"?

I have a project where I am using PHP to crawl and generate video links from the web. Now, I am looking to implement an option for users to either "Play Video" or "Download Video" when a video link is detected, along with adding a video player if the user ...

"Why isn't the reordering functionality functioning properly after the button has been clicked

I have a question regarding menu options that contain text and a button. I need to open the menu option when the button is clicked. Unfortunately, I am facing an issue where rerendering is not working when I click on the button. Here is the link for refer ...

What are the steps to integrate the aws-iot-device-sdk NPM module with Angular 11? I am encountering errors related to fs, path, and tls during the build process

I manage a browser-based Angular application that was previously on version 5.6. This app consists of two components that subscribe to an IOT topic and listen for updates without publishing. The task at hand is to upgrade to Angular 11.0.3, which I have c ...

Disabled radio buttons are appearing as if they have been chosen

While working on customizing the styles of radio buttons in a form, I encountered an issue where disabled radio buttons appeared selected even though they were supposed to show as disabled. Here is the code snippet that I used: input ::-ms-clear { ...