I need help with the javascript | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

I need help with the javascript

i am working on a dummy rgb generator project and i need help. RGB colour changes only when i refresh the page neither random rgb function button is working and rgb slider range function is not working as well. I tried to lookup the rgb range values in console log but function is not generating the expected output. Please guide me where i am doing wrong. https://code.sololearn.com/W4y2m3lhn9s8

25th Dec 2021, 1:05 PM
Edwin
Edwin - avatar
16 Answers
+ 10
https://code.sololearn.com/WU8ULH4IzV1D/?ref=app
25th Dec 2021, 3:52 PM
SAN
SAN - avatar
+ 3
I think you need to handle events, so when the value changes, it is updated in the JS code and calls a handler function.
25th Dec 2021, 1:17 PM
CGM
CGM - avatar
+ 3
Line 121, remove that paranthesis from that function, Just write like this rgb_btn.addEventListener('click',rgb_color); You don't need to call that function instantly. Just passing function name, and that's enough.
25th Dec 2021, 1:30 PM
Kelvin Paul
Kelvin Paul - avatar
+ 3
Also use addEventListener ("input", fun) and attach your element with this event to get the value of that particular slider through the event.target.value slider.addEventListener("input", (event) => console.log(event.target.value));
25th Dec 2021, 1:33 PM
Kelvin Paul
Kelvin Paul - avatar
+ 3
@Kelvin Paul, should i add event listener individually to all three sliders and pass the value to the rgb_slider function? i changed rgb_color() to rgb_color with button evenlistener and now random generator is working as expected thanks i added onchange event and passed the value but it only changes the one slider value rest two values are undefined. onChange="rgb_slider(this.value)" https://code.sololearn.com/Wqrzuqq6QP8i i will try with addeventlistener now with all three sliders
25th Dec 2021, 1:56 PM
Edwin
Edwin - avatar
25th Dec 2021, 9:40 PM
Jamari McFarlane
Jamari McFarlane - avatar
+ 3
SAN, I recommend you to replace 'onchange' to 'oninput' in line 121, 124 and 127. Then it will work better and smoothly. I hope it will help.
26th Dec 2021, 3:44 AM
Arnav Gumber
Arnav Gumber - avatar
+ 2
I also made my version, mine updates the values ​​in the html when the generator is used😌 Thanks, it helped me practice😁 https://code.sololearn.com/WjkXoXO9RX42/?ref=app
25th Dec 2021, 4:48 PM
CGM
CGM - avatar
+ 1
Math.floor(Math.random() * 256); // Returns a random integer from 0 to 255 <label for="slider_one">R: </label> <input id="slider_one" type="range" value="0" min="0" max="255" onchange="rgb_color()">
25th Dec 2021, 1:38 PM
SoloProg
SoloProg - avatar
+ 1
@SoloProg Your approach will generate random rgb colors with three different sliders onchange event . I want to change the rgb color individually using the range bar and display value changing individually of r, g and b.
25th Dec 2021, 2:03 PM
Edwin
Edwin - avatar
+ 1
Using "rgb_slider(this.value)" you are sending only the first argument to your rgb_slider function, you need to send the 3 values, I think you will need 3 "onChange" and I would do it all in JS. Otherwise you will need to change your function.
25th Dec 2021, 2:14 PM
CGM
CGM - avatar
+ 1
@Cristian Gabriel Mazzulla, How can i pass multiple arguments in "onchange" event can you please elaborate or give me an example?
25th Dec 2021, 2:26 PM
Edwin
Edwin - avatar
+ 1
Edwin , I like giving you some wats or ideas for improving the website and you can use this code input type="color" id="myColor" value="#ff0080" https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_color_value2
25th Dec 2021, 3:21 PM
SoloProg
SoloProg - avatar
+ 1
I hope this link helps you https://code.sololearn.com/WqNqDZy74b4W
25th Dec 2021, 4:44 PM
SoloProg
SoloProg - avatar
+ 1
https://code.sololearn.com/WE8K4ZmZr8SD/?ref=app This will solve your problem
27th Dec 2021, 5:29 AM
Msaligs
Msaligs - avatar
0
Hmm idk, maybe you can pass sibling values, but I don't know, im noob haha. I solved it with a "handle()" function that I made to update the values ​​and call rgb_slider(), but it's not the best way.. I think you should make rgb_slider () a function without arguments, that uses and updates the values ​​with this part of the code that you commented: // var slider_one=document.getElementById("slider_one").value; // var slider_two=document.getElementById("slider_two").value; // var slider_three=document.getElementById("slider_three").value;
25th Dec 2021, 2:38 PM
CGM
CGM - avatar