Update the color of the header background in an HTML table to match its original shade

Whenever a selection is made from the dropdown menu, a script triggers to change the background color of the selected number in the table header.

I'm trying to figure out how to revert the background color back to its original state after making a new selection. Any suggestions?

function doIt() {
  var array = [10, 20, 30, 40, 50];
  var a = document.getElementById("select").value;
  var found = array.findIndex(function (element) {
      return element >= a;
  });
document.getElementById("b").value = found;

document.getElementById('t').rows[0].cells[found].style.backgroundColor="#FF0000";
}
th {
  border: 2px solid #999;
  padding: 0.5rem;
  text-align: left;
  background-color: goldenrod;
}
  <label for="Select">Select Number:</label>
  <select id="select" onchange="doIt()";>
      <option>Select</option>
      <option>10</option>
      <option>20</option>
      <option>30</option>
      <option>40</option>
      <option>50</option>
  </select> 
  <input type="text" id="b">
</p>
<table id="t">
  <tbody>
  <tr>
  <th>10</th>
  <th>20</th>
  <th>30</th>
  <th>40</th>
  <th>50</th>
  </tr>
  </tbody>
  </table>

Answer №1

Before highlighting a cell, make sure to remove the highlight from other cells:

function highlightCell() {
  var numbers = [10, 20, 30, 40, 50];
  var selectedNumber = document.getElementById("select").value;
  var index = numbers.findIndex(function (num) {
      return num >= selectedNumber;
  });
  document.getElementById("output").value = index;
  var tableCells = document.getElementById('table').rows[0].cells;
  for(var i = 0; i < tableCells.length; i++) {
    tableCells[i].style.backgroundColor="goldenrod";
  }
  tableCells[index].style.backgroundColor="#FF0000";
}
th {
  border: 2px solid #999;
  padding: 0.5rem;
  text-align: left;
  background-color: goldenrod;
}
<label for="Select">Select Number:</label>
  <select id="select" onchange="highlightCell()";>
      <option>Select</option>
      <option>10</option>
      <option>20</option>
      <option>30</option>
      <option>40</option>
      <option>50</option>
  </select> 
  <input type="text" id="output">
</p>
<table id="table">
  <tbody>
  <tr>
  <th>10</th>
  <th>20</th>
  <th>30</th>
  <th>40</th>
  <th>50</th>
  </tr>
  </tbody>
  </table>

You may want to consider adding/removing classes instead of directly changing style properties. Classes allow you to apply multiple CSS rules as needed.

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

Alter the background color of a div contingent on the checkbox being checked within

I am in search of a solution to modify the background color of a parent div that contains a checkbox. The condition is that the background color should only change when the 'checked' attribute is present within the tag. HTML <div class="comp ...

Three.js - Exploring the Significance of "THREE" within the THREE.scene Framework

Just starting out in JavaScript and experimenting with animations using three.js library. I'm trying to grasp the meaning behind: const scene = new THREE.Scene(); Why is the "THREE" necessary in THREE.Scene? Could we not simply write const scene = n ...

The resizable function in jQuery is not functioning as expected

Within my project, the initial page includes the following code snippet: html <div id="box"> <div id="header"> <span id="title"></span> <span id="button">X</span> </div> <div id="text"> </div> ...

Position a picture next to a hamburger icon

I am having trouble aligning an image and a hamburger menu within the Header. When clicked, the list appears at the bottom of the image in a horizontal line, but it is listing vertically on the right of the image instead. How can I resolve this issue? Shou ...

Utilizing Multer for temporarily storing a file in memory before parsing and transferring it to the database

As a newcomer to programming, I am attempting to describe my issue concisely. I am utilizing multer to upload an XML file in conjunction with a Node Express server and React with .jsx. My intention is to parse the uploaded file data and post it to a Postgr ...

Is it impossible for both <a> and <name=""> to collaborate in HTML and PHP?

Is it possible to make both <a href works > and name="send" work at the same time within isset($_POST['send']) in this code? Currently, it's not functioning as intended. Is there a way to achieve this for both cases? The requirement i ...

Retrieve files from Amazon S3 using JavaScript

I'm currently working with a javascript file that's intended to download a text file from one of my S3 buckets. However, after running this file using "node file.js", nothing happens and there's no output. Is there something I'm missing ...

Is there a way in JQuery to display a message when the ul element is empty and hide it when items are added to the list?

In the title lies the question. I find myself with a ul element that lacks list items, and I'm searching for a way to present a message (perhaps a li element) so users can easily see that the list is empty. Once an item gets added to the ul, the messa ...

Overlay displayed on top of a single div element

Trying to implement a loading overlay in an Angular project within a specific div rather than covering the entire page. Here's a Fiddle illustrating my current progress: #loader-wrapper { top: 0; left: 0; width: 100%; height: 100%; ...

Incorporating an external PHP file into a div using jQuery's AJAX functionality

Working on a new project and encountering an issue with loading data from another page into index.php. I've set the time to 20 seconds but it's not functioning as expected. Any insights on what might be wrong with the query would be greatly appre ...

Changing URLs to Exclude .html Extension

Consider this idea: eliminating the .html extension from all pages on the website, like this... www.website.com/File.html > www.website.com/File www.website.com/Folder/File.html > www.website.com/Folder/File Although I've successfully accompli ...

When attempting to send coordinates to the Polyline component in React Native, an error is encountered stating that NSNumber cannot be converted to

Trying to pass coordinates from BackgroundGeolocation.getLocations() is causing an issue. Upon passing the coordinates, I encounter the following error message: JSON value '-122.02235491' of type NSNumber cannot be converted to NSDictionary I ha ...

The table is not visible when using Bootstrap Vue

Can anyone help me with displaying a table of flowers? I am having trouble making it show up on my page. Not quite sure how to use my data to get the flowers to display properly. Any guidance or advice would be greatly appreciated. <b-table> ...

AJAX Post Request Function without Form That Does Not Reset

I am currently struggling with understanding the Ajax POST method. I am attempting to make an API request, and since the response has similar parameters, I decided to create a global function that can be used by other HTML/JS pages. However, I am aware tha ...

Issue with JQuery Each function not waiting for the inner function to complete

When I have this section inside an ajax function $.each(data['Result'][0], function(Key, Value) { InputGen(Value['Type'], Value['Name'], Value['Other'], Value['Value'], function(Html){ Table = ...

Changing an element's color dynamically does not affect the color of the bullet point

When using Chrome (Version 45.0.2454.101 m), I noticed that when I add a class to a list element to change its color, the bullet point color is only updated when the window is resized or repainted. $("#a").click(function() { $("#a").addClass(&apo ...

Guide on comparing an object against an array and retrieving a specific output

If I were to create a data structure like this: const carObj = {"1234":"Corvette","4321":"Subaru","8891":"Volvo"}; And also have an array that contains the IDs: const myArray = [1234, 4321, 8891, ...

Issue: Unhandled promise rejection: BraintreeError: The 'authorization' parameter is mandatory for creating a client

I'm currently working on integrating Braintree using Angular with asp.net core. However, I've encountered an issue that I can't seem to solve. I'm following this article. The version of Angular I'm using is 14, and I have replicate ...

Can all actions in the ofType operator be waited for before calling next?

I have a list of dynamic redux actions that I need to monitor in order to trigger another action in my epic once all of them have been called. The key here is that the actions within this list can change and are not fixed. It would be ideal if the ofType o ...

Transfer data as JSON from Flask to JavaScript

Having trouble sending data from Flask to JavaScript. I have the information from the database and added it to a dictionary. Now, I want to convert this data into a JSON object in JavaScript to display it on a map. Despite using JSON.parse in JavaScript, i ...