Changing color of a div when clicking the first time and displaying an alert when clicking the second time

0

I'm having trouble creating a code that is simple, so when I click on a div it changes color and when I click it a second time it opens a alert with the name of the color.

Can be in jQuery or js

$(document).click(function() {
  $(".cor").css("background", "blue");

});

$(".cor").click(function() {
  alert("HTML: " + $("#test").html());

});
    
asked by anonymous 21.06.2017 / 17:11

1 answer

0

Here's an example, if there's any question left, put it in the comment.

$(function(){
  $('div').click(function(){
    console.log($(this).css('background-color'))
    if($(this).css('background-color') == 'rgb(204, 204, 204)'){
      $(this).css('background-color','rgb(255, 0, 0)');
    }else{
      alert('Cor da div : ' + $(this).css('background-color'));
    }
  });
});
div{
  width: 300px;
  height: 300px;
  background: rgb(204,204,204);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>
    
21.06.2017 / 17:19