1

For example : i have an input box that contains a few cell names:

<input type="text" id="occuredrooms" value="001 002 003 006">

and a table :

    <table class="table">
    <tr>
        <td class="room active">001</td>
        <td class="room active">002</td>
        <td class="room active">003</td>
        <td class="room active">004</td>
    </tr>
    <tr>
        <td class="room active">005</td>
        <td class="room active">006</td>
        <td class="room active">007</td>
        <td class="room active">008</td>
    </tr>
    <tr>
        <td class="room active">009</td>
        <td class="room active">010</td>
        <td class="room inactive">011</td>
        <td class="room active">012</td>
    </tr>
</table>
<style>
    .room.active{
        background-color: greenyellow;

    }
    .room.inactive{
        background-color: rgb(151, 49, 49);
    }
</style>

how can I change those to classes that match the input into inactive? Please help. I manage to go this far but :(( It somehow still doesn't work.

let arr = document.getElementById('occuredrooms').value.split(' ');
        var x = document.getElementsByClassName('room');
        for (i = 0; i <= arr.length - 1; i++) {
            for(j=0;j<=x.length-1;j++){
                if(x[j].innerHTML==arr[i]){
                    x[j].removeClass('active').addClass('inactive');
                }
            }
        }
Muzammil Ismail
  • 303
  • 4
  • 9
Huy Tran
  • 13
  • 4
  • "somehow still doesn't work" Please explain what happened – pxDav Oct 23 '21 at 04:59
  • The loops works but this x[j].innerHTML==arr[i] code line somehow doesn't compare anything – Huy Tran Oct 23 '21 at 05:03
  • Sadly but no, i can't find the same problem in the post you gave me. I just need to change class of few td with specific name given in the input (001, 002, 003). But anyways thank you very <3333 – Huy Tran Oct 23 '21 at 05:07
  • @HuyTran It's right here in this answer: https://stackoverflow.com/a/22270709/378779. To remove a class, you need to use `x[j].classList.remove('active')` and to add a class you need `x[j].classList.add('inactive')`. – kmoser Oct 23 '21 at 05:12

3 Answers3

0

This is just a code improvement

Your JavaScript code looks really messy. This might look more readable

let inactive_arr = document.getElementById('occuredrooms').value.split(' ');

//let rooms = document.querySelectorAll('.room');
let rooms = document.getElementsByClassName('room');

//rooms.forEach
[...rooms].forEach(el=>{
    if(inactive_arr.includes(el.innerHTML)) {
        el.classList.remove('active');
        el.classList.add('inactive');
    }
})

The commented line is another way of doing it.

pxDav
  • 1,498
  • 9
  • 18
0

HI working example

I've made the changes and given you a working version

HTML

<input type="text" id="occuredrooms" value="" onChange="updateTable()">
<table class="table">
    <tr>
        <td class="room active">001</td>
        <td class="room active">002</td>
        <td class="room active">003</td>
        <td class="room active">004</td>
    </tr>
    <tr>
        <td class="room active">005</td>
        <td class="room active">006</td>
        <td class="room active">007</td>
        <td class="room active">008</td>
    </tr>
    <tr>
        <td class="room active">009</td>
        <td class="room active">010</td>
        <td class="room inactive">011</td>
        <td class="room active">012</td>
    </tr>
</table>

javascript


function updateTable(e){
  if(!document.getElementById('occuredrooms').value){
        var x = document.getElementsByClassName('room');
      for(j=0;j<=x.length-1;j++){
          x[j].classList.remove('active');
          x[j].classList.add('inactive');
    }
  }
    let arr = document.getElementById('occuredrooms').value.split(' ');
  var x = document.getElementsByClassName('room');
  for (i = 0; i <= arr.length - 1; i++) {
    for(j=0;j<=x.length-1;j++){
      if(x[j].innerHTML==arr[i]){
        x[j].classList.add('active');
        x[j].classList.remove('inactive');
      }
    }
  }
}
  var x = document.getElementsByClassName('room');
    for(j=0;j<=x.length-1;j++){
        x[j].classList.remove('active');
        x[j].classList.add('inactive');
    }

CSS


    .room.active{
        background-color: greenyellow;

    }
    .room.inactive{
        background-color: rgb(151, 49, 49);
    }
Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28
-1

Maybe this example will help ?

document.getElementById('occuredrooms').addEventListener("input", (e) => {
  let arr = e.target.value.split(' ');
  document.querySelectorAll("table td.room").forEach((el) => {
    if (arr.indexOf(el.innerHTML) >= 0) {
      el.classList.remove('active');
      el.classList.add('inactive');
    } else {
      el.classList.remove('inactive');
      el.classList.add('active');
    }
  })
})
<input type="text" id="occuredrooms" value="001 002 003 006">

<table class="table">
  <tr>
    <td class="room active">001</td>
    <td class="room active">002</td>
    <td class="room active">003</td>
    <td class="room active">004</td>
  </tr>
  <tr>
    <td class="room active">005</td>
    <td class="room active">006</td>
    <td class="room active">007</td>
    <td class="room active">008</td>
  </tr>
  <tr>
    <td class="room active">009</td>
    <td class="room active">010</td>
    <td class="room inactive">011</td>
    <td class="room active">012</td>
  </tr>
</table>
<style>
  .room.active {
    background-color: greenyellow;
  }
  
  .room.inactive {
    background-color: rgb(151, 49, 49);
  }
</style>
Oleg Barabanov
  • 2,468
  • 2
  • 8
  • 17