I'm trying to create three sliders with R, G, B values and that gives in result the values you're sliding and a div with the resulting color.
I'm having a really hard time and I don't get why. Please keep in mind I'm really new to jQuery, so there might (and probably there are) some really stupid errors in the syntax.
$(document).ready(function() {
$(".slider").addEventListener("input", function() {
let red = $("#red").value;
let green = $("#green").value;
let blue = $("#blue").value;
$("#colorbox").css("background-color", "rgb(" + red + "," + green + "," + blue + ")");
$("#rgboutput").text = "RGB(" + red + "," + green + "," + blue + ")";
$("#hexoutput").text = "HEX(#" + rgbtohex(red) + rgbtohex(green) + rgbtohex(blue) + ")";
}, false)
function rgbtohex(color) {
var hex = parseInt(color).toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
})
body {
background-color: #0075FF;
}
.container {
background-color: #ffffff;
margin: auto;
width: 30%;
padding: 20px 0;
box-shadow: 20px 20px 30px rgba(0, 0, 0, 0.2);
font-family: 'Work Sans', sans-serif;
font-size: 20px;
font-weight: 500;
color: #142b37;
border-radius: 5px;
}
.center {
display: grid;
justify-items: center;
}
.wrapper {
width: 100%;
text-align: center;
}
input[type="range"] {
display: inline-block;
width: 60%;
margin-left: 10px;
margin-top: 25px;
}
.output {
display: inline-block;
text-align: center;
margin: 20px 0;
background-color: blue;
color: #ffffff;
padding: 10px 30px;
font-size: 18px;
letter-spacing: 0.5px;
border-radius: 2px;
}
#colorbox {
width: 3em;
height: 3em;
margin: 3% solid black;
background-color: black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<div class="container">
<div class="center">
<div class="wrapper">
<p>R</p><input type="range" min="0" max="255" value="0" class="slider" id="red" autocomplete="off">
</div>
<div class="wrapper">
<p>G</p><input type="range" min="0" max="255" value="0" class="slider" id="green" autocomplete="off">
</div>
<div class="wrapper">
<p>B</p><input type="range" min="0" max="255" value="0" class="slider" id="blue" autocomplete="off">
</div>
<div class="output" id="rgboutput">
<p>RGB(0, 0, 0)</p>
</div>
<div class="output" id="hexoutput">
<p>HEX(#000000)</p>
</div>
<div id="colorbox"></div> <br>
</div>
</div>