How can I append and remove a Font Awesome icon in a click event (Javascript)

Viewed 541

I'm trying to write JavaScript code that will apply a FA tick icon to a day when clicked if there isn't already a tick there, and remove the tick if there is one. I have written code that appends the icon but cannot figure out how to remove it

here's what I have

HTML and JS:

const days = document.getElementsByClassName('day')
for (let i = 0; i < days.length; i++) {
    days[i].addEventListener('click', function(e){
        const tick = document.createElement('i');
        tick.classList = 'fas fa-check-square fa-4x'
        this.append(tick);
    });    
}
<div class="day">1</div>
<div class="day">2</div>
<div class="day">3</div>
<div class="day">4</div>
<div class="day">5</div>
<div class="day">6</div>
<div class="day">7</div>
<div class="day">8</div>
<div class="day">9</div>
<div class="day">10</div>

3 Answers

You can check if the element i is already exists or not, if not exists then create the element and add the classes to the element. If present then remove the element.

You can try the following way:

const days = document.getElementsByClassName('day')
for (let i = 0; i < days.length; i++) {
    days[i].addEventListener('click', function(e){        
        if(!this.querySelectorAll('i').length){ // check if i is not exist
          const tick = document.createElement('i');
          this.append(tick);
          tick.classList.add('fas', 'fa-check-square', 'fa-4x'); // add classes
        }
        else{
          var elem = this.querySelector('i');
          elem.parentNode.removeChild(elem); // remove i
        }
        
    })    
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<div class="day">1</div>
<div class="day">2</div>
<div class="day">3</div>
<div class="day">4</div>
<div class="day">5</div>
<div class="day">6</div>
<div class="day">7</div>
<div class="day">8</div>
<div class="day">9</div>
<div class="day">10</div>

You can try to add the id to your icon & check for id to determine it exists or not.

const days = document.getElementsByClassName('day')
for (let i = 0; i < days.length; i++) {
  days[i].addEventListener('click', function(e){
    const check_square_icon = `check-square-icon-${i}`
    if(document.contains(document.getElementById(check_square_icon))) {
        document.getElementById(check_square_icon).remove();
    } else {
      const tick = document.createElement('i');
      tick.id = `check-square-icon-${i}`
      tick.classList = 'fas fa-check-square fa-4x'
      this.append(tick);
    }
    
  })    
}

As I understand it, you need to remove tag <i> if it has already been added. To do this, check for the presence of tag <div> in the current class .day:

if (!this.getElementsByTagName("i").length > 0) { ... }

If the tag exists, then delete it using the removeChild() method:

this.removeChild(this.getElementsByTagName("i")[0]);

const days = document.getElementsByClassName("day");
for (let i = 0; i < days.length; i++) {
    days[i].addEventListener("click", function (e) {
        if (!this.getElementsByTagName("i").length > 0) {
            const tick = document.createElement("i");
            tick.classList = "fas fa-check-square fa-4x";
            this.append(tick);
        } else {
            this.removeChild(this.getElementsByTagName("i")[0]);
        }
    });
}
<div class="day">1</div>
<div class="day">2</div>
<div class="day">3</div>
<div class="day">4</div>
<div class="day">5</div>
<div class="day">6</div>
<div class="day">7</div>
<div class="day">8</div>
<div class="day">9</div>
<div class="day">10</div>

Related