0

I have a server generated template that contains multiple elements with ids book_cat_id, book_cat_id2 etc that I need to find and change their inline background colours to match corresponding categories

data-cat_label="Fiction" can have any one of the categories Fiction, Fantasy, Comic, History, Technical

Is there is more efficient way of doing this for multiple colours?

    const colors =	{
     fiction: "#ff7477",  
     fantasy: "#7dbb65",  
     technical: "#BC9DCA", 
     comic: "#00A2E0", 
     history: "#ff0099", 
     health: "#f59e2e" 
    }

    let id1   = book_cat_id.getAttribute("data-cat_label");
    let id2   = book_cat_id2.getAttribute("data-cat_label");

    if(id1 === 'Fiction') {
          book_cat_id.style.backgroundColor = colors.fiction;
      }else if (id1 === 'Fantasy') {
           book_cat_id.style.backgroundColor = colors.fantasy;
      }else if (id1 === 'Comic') {
           book_cat_id.style.backgroundColor = colors.comic;
      }else if (id1 === 'History') {
           book_cat_id.style.backgroundColor = colors.history;
      }
      
    if(id2 === 'Fiction') {
          book_cat_id2.style.backgroundColor = colors.fiction;
      }else if (id2 === 'Fantasy') {
           book_cat_id2.style.backgroundColor = colors.fantasy;
      }else if (id2 === 'Comic') {
           book_cat_id2.style.backgroundColor = colors.comic;
      }else if (id2 === 'History') {
           book_cat_id2.style.backgroundColor = colors.history;
      }
    <table>
      <tr>
        <td id="book_cat_id" data-cat_label="Fiction" style="background-color:#ff0000;">
          Book
        </td>
      </tr>
      <tr>
        <td id="book_cat_id2" data-cat_label="Fantasy" style="background-color:#ff0000;">
          Book 
        </td>
      </tr>
    </table>
      

 
    



           

5
  • put the ids in an array and in your case you cna sue a switch stament Commented May 21, 2019 at 17:44
  • on a 1:1 switch, a look up table is the way to go: book_cat_id.style.backgroundColor = ({'History': colors.history, 'Fantasy': colors.fantasy})[id1] ; add keys as needed. Commented May 21, 2019 at 17:44
  • if you name the ids corrently you can also do = colors[id] Commented May 21, 2019 at 17:45
  • 2
    book_cat_id.style.backgroundColor = colors[id1.toLowerCase()], assuming all of the possible labels are present as keys in colors... Commented May 21, 2019 at 17:45
  • if you have access to the server id also think about adding classes to the elements then you can do document.getElementsByClassName("book_cat").forEach(el=>...) Commented May 21, 2019 at 17:47

6 Answers 6

5

In this case, you could do something like this.

Instead of:

if(id2 === 'Fiction') {
    book_cat_id2.style.backgroundColor = colors.fiction;
}else if (id2 === 'Fantasy') {
     book_cat_id2.style.backgroundColor = colors.fantasy;
}else if (id2 === 'Comic') {
     book_cat_id2.style.backgroundColor = colors.comic;
}else if (id2 === 'History') {
     book_cat_id2.style.backgroundColor = colors.history;
}

You could do:

book_cat_id2.style.backgroundColor = colors[id2.toLowerCase()]
Sign up to request clarification or add additional context in comments.

Comments

3

You're almost there. Replace the current Javascript with the following:

const colors ={
    fiction: "#ff7477",  
    fantasy: "#7dbb65",  
    technical: "#BC9DCA", 
    comic: "#00A2E0", 
    history: "#ff0099", 
    health: "#f59e2e" 
}

let id1 = book_cat_id.getAttribute("data-cat_label");
let id2 = book_cat_id2.getAttribute("data-cat_label");

// Replaces if statements with a direct lookup by ID
book_cat_id.style.backgroundColor = colors[id1.toLowerCase()];
book_cat_id2.style.backgroundColor = colors[id2.toLowerCase()];

Comments

3

Since you already have the key names, just use toLowerCase()

const colors = {
  fiction: "#ff7477",
  fantasy: "#7dbb65",
  technical: "#BC9DCA",
  comic: "#00A2E0",
  history: "#ff0099",
  health: "#f59e2e"
}

let id1 = book_cat_id.getAttribute("data-cat_label").toLowerCase();
let id2 = book_cat_id2.getAttribute("data-cat_label").toLowerCase();
book_cat_id.style.backgroundColor = colors[id1];
book_cat_id2.style.backgroundColor = colors[id2];
<table>
  <tr>
    <td id="book_cat_id" data-cat_label="Fiction" style="background-color:#ff0000;">
      Book
    </td>
  </tr>
  <tr>
    <td id="book_cat_id2" data-cat_label="Fantasy" style="background-color:#ff0000;">
      Book
    </td>
  </tr>
</table>

Comments

2

You can create 2 arrays - typeName and typeColor. typeName[0] will refer to "Fiction" and typeColor[0] will refer to "#ff7477". You can then make a for loop and loop through them like this:

const typeColors = [
 "#ff7477",  
 "#7dbb65",  
 "#BC9DCA", 
 "#00A2E0", 
 "#ff0099", 
 "#f59e2e" 
];

const typeNames = [
 "Fiction",
 "Fantasy",
 "Technical",
 "Comic",
 "History",
 "Health"
];

let id1   = book_cat_id.getAttribute("data-cat_label");
let id2   = book_cat_id2.getAttribute("data-cat_label");

for (var i = 0; i<typeColors.length; i++) {
    if (id1 == typeNames[i]) {
        book_cat_id.style.backgroundColor = typeColors[i];
    }
    if (id2 == typeNames[i]) {
        book_cat_id2.style.backgroundColor = typeColors[i];
    }
}

Comments

1

You can use computed property access of object using [] notation

const colors =	{
 fiction: "#ff7477",  
 fantasy: "#7dbb65",  
 technical: "#BC9DCA", 
 comic: "#00A2E0", 
 history: "#ff0099", 
 health: "#f59e2e" 
}

let id1   = book_cat_id.getAttribute("data-cat_label");
let id2   = book_cat_id2.getAttribute("data-cat_label");

book_cat_id.style.backgroundColor = colors[id1.toLowerCase()]
book_cat_id2.style.backgroundColor = colors[id2.toLowerCase()]
<table>
  <tr>
    <td id="book_cat_id" data-cat_label="Fiction" style="background-color:#ff0000;">
      Book
    </td>
  </tr>
  <tr>
    <td id="book_cat_id2" data-cat_label="Fantasy" style="background-color:#ff0000;">
      Book 
    </td>
  </tr>
</table>

Comments

1

If data-cat_label is named same as property in colors object, you can access the object property using bracket(colors[id1]) of dot notation(colors.id1).

 let id1   = book_cat_id.getAttribute("data-cat_label").toLowerCase();
 let id2   = book_cat_id2.getAttribute("data-cat_label").toLowerCase();
 book_cat_id.style.backgroundColor = colors[id1];
 book_cat_id2.style.backgroundColor = colors[id2];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.