2

I'm still a beginner in javascript and CSS, I have a bookmark heart icon I want to show alert depending if the icon checked or unchecked.

$('button').on('click', function(){
  $(this).toggleClass('faved');
  if ($(this).hasClass('faved')) {
  alert("red");
} else {
  alert("empty");
}
  
});
@yellow: #FFAC33;
@gray: #CCC;
@red: #E86C6C;

button#favorite {
  background: transparent;
  border: 0;
  span {
    padding: 20px;
    font-size: 70px;
    font-weight: normal;
    color: @gray;
    position: relative;
    span {
      position: absolute !important;
      top: 0;
      left: 0;
      font-size: 70px;
    }
  }
  
}


@keyframes favorite {
  .favorite;
} 

@-webkit-keyframes favorite {
  .favorite;
}

@keyframes favoriteHollow {
  .favoriteHollow;
}

@-webkit-keyframes favoriteHollow {
  .favoriteHollow;
}


button#heart {
  background: transparent;
  border: 0;
  span {
    padding: 20px;
    font-size: 70px;
    font-weight: normal;
    color: @gray;
    position: relative;
    span {
      position: absolute !important;
      top: 0;
      left: 0;
      font-size: 70px;
    }
  }
  &.faved {
    span {
      -webkit-animation: heart 0.5s;      
      animation: heart 0.5s;      
      color: @red;
      span {
        z-index: 1000;
        -webkit-animation: heartHollow 0.5s;      
        animation: heartHollow 0.5s;      
      }
    }
  }
}

.heart {
   {
    transform: scale(1);
  }
  {
    transform: scale(1.2);
    color: @red;
  }
   {
    transform: scale(1.4);
    color: @red;
  }
   {
    transform: scale(1);
    color: @red;
  }
}

.heartHollow {
   {
    transform: scale(1);
    opacity: 1;
  }
   {
    transform: scale(1.4);
    opacity: 0.5;
  }
  {
    transform: scale(1.6);
    opacity: 0.25;
  }
  {
    transform: scale(2);
    opacity: 0;
    display: none;
  }
}

@keyframes heart {
  .heart;
} 

@-webkit-keyframes heart {
  .heart;
} 

@keyframes heartHollow {
  .heartHollow;
}

@-webkit-keyframes heartHollow {
  .heartHollow;
}
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css" rel="stylesheet">

<button id="heart">
  <span class="glyphicon glyphicon-heart">
    <span class="glyphicon glyphicon-heart">
    </span>
  </span>
</button>

I know your time is valuable and I appreciate your attention and thank you in advance

1
  • 1
    Do you mean if ($(this).hasClass('faved')) {}? Commented Apr 8, 2019 at 11:08

1 Answer 1

1

for checking class you have to use .hasClass('your class name') or if you want to check background color if($(this).css('background') == "red")

 $('button').on('click', function(){
      $(this).toggleClass('faved');
      if ($(this).hasClass('faved')) {
          alert("red");
      } else {
          alert("empty");
      }        
    });
Sign up to request clarification or add additional context in comments.

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.