Font Awesome Icon Will Not Append Before Tool Tip

Viewed 82

I have created a risk matrix with some simple HTML and very simple JS. There are three different categories where items can get posted to in a tooltip, Red, Yellow, or Green. These all have to do with the Severity(Likelihood * Consequence) of each item posted.

As of now, the information only posts in a tooltip on whichever (x, y) coordinates it belongs, the hassle is the square it is appended to has to be hovered over to show the tool tip, which is kind of inconvenient. I want to append different font awesome icons depending on the severity like so:

<i class="fa fa-exclamation" aria-hidden="true"></i> This for items appended in Green Squares. <i class="fa fa-exclamation-circle" aria-hidden="true"></i> This for items appended in Yellow Squares. <i class="fa fa-exclamation-triangle" aria-hidden="true"></i> This for items appended in Red Squares.

In my snippet you can see where I commented out trying to append the first one to test it, it tells me that "<a class='gotoLine' href='#236:87'>236:87</a> Uncaught TypeError: document.getElementById(...).innerHTML is not a function"

UPDATE Yash provided a working appending method which answers part of the question. In the new updated snippet, I created a conditional to append three different types of Font Awesome Icons based on the values of ConsequencexLikelihood, and now nothing will append to the table

const dataFromFetch = {
  "likelihood" : 4, 
  "consequence": 3, 
  "severity" : 12, 
  "category": "Category Name", 
  "Status": "Active", 
  "Title": "This is the Title of the Occurrence", 
  "Priority": "High"
}

if (dataFromFetch.consequence === 1 && dataFromFetch.likelihood <= 5){
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).innerHTML='<i class="fa fa-exclamation" aria-hidden="true" style="font-color: black;"></i>';
} if (dataFromFetch.consequence === 2 && dataFromFetch.likelihood <= 3){
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).innerHTML='<i class="fa fa-exclamation" aria-hidden="true" style="font-color: black;"></i>';
} if (dataFromFetch.consequence === 3 && dataFromFetch.likelihood <= 2){
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).innerHTML='<i class="fa fa-exclamation" aria-hidden="true" style="font-color: black;"></i>';
} if (dataFromFetch.consequence === 4 && dataFromFetch.likelihood === 1){
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).innerHTML='<i class="fa fa-exclamation" aria-hidden="true" style="font-color: black;"></i>';
} if (dataFromFetch.consequence === 2 && dataFromFetch.likelihood >= 4){
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).innerHTML='<i class="fa fa-exclamation-circle" aria-hidden="true" style="font-color: black;"></i>';
} if (dataFromFetch.consequence === 3 && dataFromFetch.likelihood > 2 && dataFromFetch < 5){
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).innerHTML='<i class="fa fa-exclamation-circle" aria-hidden="true" style="font-color: black;"></i>';
} if (dataFromFetch.consequence === 4 && dataFromFetch.likelihood > 1 && dataFromFetch.likelihood < 4){
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).innerHTML='<i class="fa fa-exclamation-circle" aria-hidden="true" style="font-color: black;"></i>';
} if (dataFromFetch.consequence === 5 && dataFromFetch.likelihood < 3){
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).innerHTML='<i class="fa fa-exclamation-circle" aria-hidden="true" style="font-color: black;"></i>';
} if (dataFromFetch.consequence === 5 && dataFromFetch.likelihood >= 3){
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).innerHTML='<i class="fa fa-exclamation-triangle" aria-hidden="true"></i>';
} if (dataFromFetch.consequence === 4 && dataFromFetch.likelihood >= 4){
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).innerHTML='<i class="fa fa-exclamation-triangle" aria-hidden="true"></i>';
} if (dataFromFetch.consequence === 3 && dataFromFetch.likelihood === 5){
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).innerHTML='<i class="fa fa-exclamation-triangle" aria-hidden="true"></i>';
} 

document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).innerHTML='<i class="fa fa-exclamation" aria-hidden="true" style = "color: black;"></i>';
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).title =  
  dataFromFetch.category + '\n' + 
  dataFromFetch.Status + '\n' + 
  dataFromFetch.Title + '\n' +
  dataFromFetch.Priority
.box {
  position: relative;
  background-color: lightgrey;
  color: #fff;
  border-radius: 0px;
  padding: 20px;
  font-size: 150%;
  border: 1px solid black;
}

.wrapper {
  margin: 60px 0 90px 90px;
  display: grid;
  grid-gap: 0;
  grid-template-columns: repeat(5, 100px);
  grid-template-rows: repeat(5, 100px);
  grid-auto-flow: column;
}

#red {
  background-color: red;
}
#yellow {
  background-color: yellow;
}
#green {
  background-color: green;
}
section {
  position: relative;
  width: 600px;
}
p.likelihood {
   transform: rotate(-90deg) translateY(-50%);
   transform-origin: top;
   position: absolute;
   top: 50%;
   left: -20px;
   font-size: 30px;
   margin: 0;
}

p.consequence {
   font-size: 30px;
   position: absolute;
   transform: translateX(-50%);
   left: calc(50% + 45px);
   bottom: -60px;
   margin: 0;
}

.numbers-container {
  position: absolute;
  display: flex;
}

.numbers-container-x {
  padding-top: 10px;
  left: 90px;
  bottom: -25px; 
}

.numbers-container-x .number {
  width: 100px;
  text-align: center;
}

.numbers-container-y {
  flex-direction: column-reverse;
  left: 70px;
  top: 0;
}

.numbers-container-y .number {
  height: 100px;
  line-height: 100px;
}
<html lang="en">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="Risk Management Matrix">

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
        <!-- Insert any extra scripts here -->
    </head>
    <body>
    <section>
        <div class="wrapper">
            <div class="box box1" id="5-1" style="background-color: #329932;">
            </div>
            <div class="box box1" id="4-1" style="background-color: #329932;">
            </div>
            <div class="box box1" id="3-1" style="background-color: #329932;">
            </div>
            <div class="box box1" id="2-1" style="background-color: #329932;">
            </div>
            <div class="box box1" id="1-1" style="background-color: #329932;">
            </div>
            <div class="box box1" id="5-2" style="background-color: #ffff32;">
        
            </div> 
            <div class="box box1" id="4-2" style="background-color: #ffff32;">
        
            </div>
            <div class="box box1" id="3-2" style="background-color: #329932;">
        
            </div>
            <div class="box box1" id="2-2" style="background-color: #329932;">
        
            </div>
            <div class="box box1" id="1-2" style="background-color: #329932;">
        
            </div>
            <div class="box box1" id="5-3"  style="background-color: #ff3232;">
        
            </div>
            <div class="box box1" id="4-3" style="background-color: #ffff32;">
        
            </div>
            <div class="box box1" id="3-3" style="background-color: #ffff32;">
        
            </div>
            <div class="box box1" id="2-3" style="background-color: #329932;">
        
            </div>
            <div class="box box1" id="1-3" style="background-color: #329932;">
        
            </div>
            <div class="box box1" id="5-4" style="background-color: #ff3232;">
        
            </div>
            <div class="box box1" id="4-4" style="background-color: #ff3232;">
        
            </div>
            <div class="box box1" id="3-4" style="background-color: #ffff32;">
        
            </div>
            <div class="box box1" id="2-4" style="background-color: #ffff32;">
        
            </div>
            <div class="box box1" id="1-4" style="background-color: #329932;">
        
            </div>
            <div class="box box1" id="5-5" style="background-color: #ff3232;">
        
            </div>
            <div class="box box1" id="4-5" style="background-color: #ff3232;">
        
            </div>
            <div class="box box1" id="3-5" style="background-color: #ff3232;">
        
        
            </div>
            <div class="box box1" id="2-5" style="background-color: #ffff32;">
        
            </div>
            <div class="box box1" id="1-5" style="background-color: #ffff32;">
        
            </div>
        </div>
        
        <div class="numbers-container numbers-container-y">
            <div class="number">1</div>
            <div class="number">2</div>
            <div class="number">3</div>
            <div class="number">4</div>
            <div class="number">5</div>
        </div>
        
        <div class="numbers-container numbers-container-x">
            <div class="number">1</div>
            <div class="number">2</div>
            <div class="number">3</div>
            <div class="number">4</div>
            <div class="number">5</div>
        </div>
        
        <p class="likelihood">
            Likelihood
        </p>
        <p class="consequence">
            Consequence
        </p>
        </section>
    </body>
    </html>

1 Answers

You can directly assign a value to innerHTML, as it's not a function. So you need to remove the parenthesis and just simply assign a value to it.

const dataFromFetch = {
  "likelihood" : 4, 
  "consequence": 3, 
  "severity" : 12, 
  "category": "Category Name", 
  "Status": "Active", 
  "Title": "This is the Title of the Occurrence", 
  "Priority": "High"
}
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).innerHTML='<i class="fa fa-exclamation" aria-hidden="true"></i>';
document.getElementById( dataFromFetch.likelihood + '-' + dataFromFetch.consequence ).title =  
  dataFromFetch.category + '\n' + 
  dataFromFetch.Status + '\n' + 
  dataFromFetch.Title + '\n' +
  dataFromFetch.Priority
.box {
  position: relative;
  background-color: lightgrey;
  color: #fff;
  border-radius: 0px;
  padding: 20px;
  font-size: 150%;
  border: 1px solid black;
}

.wrapper {
  margin: 60px 0 90px 90px;
  display: grid;
  grid-gap: 0;
  grid-template-columns: repeat(5, 100px);
  grid-template-rows: repeat(5, 100px);
  grid-auto-flow: column;
}

#red {
  background-color: red;
}
#yellow {
  background-color: yellow;
}
#green {
  background-color: green;
}
section {
  position: relative;
  width: 600px;
}
p.likelihood {
   transform: rotate(-90deg) translateY(-50%);
   transform-origin: top;
   position: absolute;
   top: 50%;
   left: -20px;
   font-size: 30px;
   margin: 0;
}

p.consequence {
   font-size: 30px;
   position: absolute;
   transform: translateX(-50%);
   left: calc(50% + 45px);
   bottom: -60px;
   margin: 0;
}

.numbers-container {
  position: absolute;
  display: flex;
}

.numbers-container-x {
  padding-top: 10px;
  left: 90px;
  bottom: -25px; 
}

.numbers-container-x .number {
  width: 100px;
  text-align: center;
}

.numbers-container-y {
  flex-direction: column-reverse;
  left: 70px;
  top: 0;
}

.numbers-container-y .number {
  height: 100px;
  line-height: 100px;
}
<html lang="en">
    <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="Risk Management Matrix">

        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" />
        <!-- Insert any extra scripts here -->
    </head>
    <body>
    <section>
        <div class="wrapper">
            <div class="box box1" id="5-1" style="background-color: #329932;">
            </div>
            <div class="box box1" id="4-1" style="background-color: #329932;">
            </div>
            <div class="box box1" id="3-1" style="background-color: #329932;">
            </div>
            <div class="box box1" id="2-1" style="background-color: #329932;">
            </div>
            <div class="box box1" id="1-1" style="background-color: #329932;">
            </div>
            <div class="box box1" id="5-2" style="background-color: #ffff32;">
        
            </div> 
            <div class="box box1" id="4-2" style="background-color: #ffff32;">
        
            </div>
            <div class="box box1" id="3-2" style="background-color: #329932;">
        
            </div>
            <div class="box box1" id="2-2" style="background-color: #329932;">
        
            </div>
            <div class="box box1" id="1-2" style="background-color: #329932;">
        
            </div>
            <div class="box box1" id="5-3"  style="background-color: #ff3232;">
        
            </div>
            <div class="box box1" id="4-3" style="background-color: #ffff32;">
        
            </div>
            <div class="box box1" id="3-3" style="background-color: #ffff32;">
        
            </div>
            <div class="box box1" id="2-3" style="background-color: #329932;">
        
            </div>
            <div class="box box1" id="1-3" style="background-color: #329932;">
        
            </div>
            <div class="box box1" id="5-4" style="background-color: #ff3232;">
        
            </div>
            <div class="box box1" id="4-4" style="background-color: #ff3232;">
        
            </div>
            <div class="box box1" id="3-4" style="background-color: #ffff32;">
        
            </div>
            <div class="box box1" id="2-4" style="background-color: #ffff32;">
        
            </div>
            <div class="box box1" id="1-4" style="background-color: #329932;">
        
            </div>
            <div class="box box1" id="5-5" style="background-color: #ff3232;">
        
            </div>
            <div class="box box1" id="4-5" style="background-color: #ff3232;">
        
            </div>
            <div class="box box1" id="3-5" style="background-color: #ff3232;">
        
        
            </div>
            <div class="box box1" id="2-5" style="background-color: #ffff32;">
        
            </div>
            <div class="box box1" id="1-5" style="background-color: #ffff32;">
        
            </div>
        </div>
        
        <div class="numbers-container numbers-container-y">
            <div class="number">1</div>
            <div class="number">2</div>
            <div class="number">3</div>
            <div class="number">4</div>
            <div class="number">5</div>
        </div>
        
        <div class="numbers-container numbers-container-x">
            <div class="number">1</div>
            <div class="number">2</div>
            <div class="number">3</div>
            <div class="number">4</div>
            <div class="number">5</div>
        </div>
        
        <p class="likelihood">
            Likelihood
        </p>
        <p class="consequence">
            Consequence
        </p>
        </section>
    </body>
    </html>

Related