Suppose I have a block. He has his own styles. I want that when I left-click on a block, it changes the background to black. How to make js or jquery?

#first { width: 50px; height: 50px; background-color: #FA174C; } 
  <div id="first"></div> 

    2 answers 2

     $(function() { // Левая кнопка $('#first').click(function() { $(this).css('background-color', 'black'); }); // Правая кнопка $('#first').contextmenu(function() { $(this).css('background-color', 'blue'); return false; }); }); 
     #first { width: 50px; height: 50px; background-color: #FA174C; } 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="first"></div> 

       document.getElementById("first").onmousedown = function(event) { if (event.button == 2) { //if (event.which == 3) { alert("right clicked!"); } } 
       #first { width: 50px; height: 50px; background-color: #FA174C; } 
       <div id="first"></div> 

      button == 0 - left click, button == 1 - click on the wheel / middle button, button == 2 - right click

      PS Somewhere (I do not remember where) it is recommended to use which , instead of button .

      which == 1 - left click, which == 2 - click on the wheel / middle button, which == 3 - right click