How do you get a div block color on js?



  • How can we get a color? div block(s)background cssand release, for example alert()? For example: ff7659 After the click on the button.

    Code: https://jsfiddle.net/wa2r1q7p/1/

    Html:

    <div id="color_bk"></div>
      <br><br>
    <div id="but_color">Получить цвет</div>
    

    Js:

    $("#but_color").click(function () {
    
    //alert();
    

    });

    Css:

    #color_bk{
    height:150px;
    width:150px;
    background: #ff7659;
    }

    #but_color{
    height:20px;
    width:150px;
    background: #5ca7df;
    padding: 5px 0;
    text-align: center;
    color: #000;
    font-size:16px;
    font-weight:bold;
    cursor: pointer;
    }



  • var hexDigits = new Array
            ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"); 
    
    function convertToHex(rgb) {
     rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
     return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
    }
    
    function hex(x) {
      return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
     }
    

    https://jsfiddle.net/wa2r1q7p/3/ under review stackover

    It's if it's the kind of color you need, and it's easy. $('#color_bk').css('background-color')




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2