Search code examples
javascripthtmlonmouseovergrayscaleonmouseout

HTML with Javascript - Apply grayscale to images in a table, then mouseover images to go back to colored version


I had a question about using the mouseover / mouseout event in javascript along with applying grayscale to a table. The question says that I must first making an image grid (table) all gray in html. Then I need to add javascript to the html so that when I mouse over the image, the image turns into a colored image, and when I mouse out from the image, the image reverts back into a gray image. The problem said no CSS is allowed, so only using javascript and html, if possible. Thank you so much in advance for the help, I really appreciate it!

Here is some of my code below (the table images need to start from grayscale, then apply/remove the grayscale when using the mouseover event. So far the mouseover effect only works on the first image. And I also don't know how to apply a grayscale filter over the whole table first).

function image_grayscale() {
  document.getElementById("image").style.filter = "grayscale(100%)";
}

function remove_grayscale() {
  document.getElementById("image").style.filter = "grayscale(0%)";
}
<div class="table">
  <table border="3" align=center width="600" height="200">
    <tr style="width:1" ;style="height:10%" ; bgcolor="white">
      <td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
        <img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
      </td>

      <td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
        <img id="image" style="grayscale" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
      </td>

      <td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
        <img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
      </td>

      <td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
        <img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
      </td>

      <td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
        <img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
      </td>

      <td onmouseover="remove_grayscale()" onmouseout="image_grayscale()">
        <img id="image" src="https://picsum.photos/id/1067/100/100" width="100" height="100" />
      </td>
    </tr>
  </table>


Solution

    1. An id attribute must be unique.
    2. Don't clutter the HTML more than necessary. It should be really easy to read.
    3. Use addEventListener instead of onmouseover.
    4. Method names are usually written with kebabCase (see the new method I added).
    5. Don't repeat code. Instead, refactor similar code into a new method.

    let table = document.getElementById('greyscaleTable')
    
    table.addEventListener('mouseover', remove_grayscale);
    table.addEventListener('mouseout', image_grayscale);
    
    function image_grayscale(event) {
      let element = event.target;
      changeGrayscale('100%', element);
    }
    
    function remove_grayscale(event) {
      let element = event.target;
      changeGrayscale('0%', element);
    }
    
    function changeGrayscale(amount, element) {
      let isGrayscaleImage = element.classList.contains('grayscale');
      
      if (isGrayscaleImage) {
        element.style.filter = `grayscale(${amount})`;  
      }
    }
    #greyscaleTable img {
      width: 100px;
      height: 100px;
    }
    <div id="greyscaleTable" class="table">
      <table border="3" align=center width="600" height="200">
        <tr>
          <td>
            <img class="grayscale" style="filter: grayscale(100%)"  src="https://picsum.photos/id/1067/100/100" />
          </td>
    
          <td>
            <img class="grayscale" style="filter: grayscale(100%)" src="https://picsum.photos/id/1067/100/100" />
          </td>
    
          <td>
            <img class="grayscale" style="filter: grayscale(100%)" src="https://picsum.photos/id/1067/100/100"/>
          </td>
    
          <td>
            <img class="grayscale" style="filter: grayscale(100%)" src="https://picsum.photos/id/1067/100/100"/>
          </td>
    
          <td>
            <img class="grayscale" style="filter: grayscale(100%)" src="https://picsum.photos/id/1067/100/100" />
          </td>
    
          <td>
            <img class="grayscale" style="filter: grayscale(100%)" src="https://picsum.photos/id/1067/100/100" />
          </td>
        </tr>
      </table>