JavaScript JavaScript and the DOM Getting a Handle on the DOM Select a Page Element By Its ID

Adding a random RGB generator function when clicking on the button. It doesn't work!

Hello there,

I'll be honest here and I am running ahead. I am looking at my past notes and I finally wanted to implement a random RGB generator function in the DOM. However, it doesn't work. My code is the following:

var red= " ";
var green = '';
var blue = '';
var rgbColor = " ";

red = Math.floor(Math.random() * 256 );
green = Math.floor(Math.random() * 256 );
blue = Math.floor(Math.random() * 256 );

function RandomRGB () {
rgbColor = 'rgb(' +  red + ',' +  green + ',' +  blue + ')';
return rgbColor
}

const myHeading = document.getElementById("myHeading");
const myTextInput =  document.getElementById("myTextInput");
const myButton = document.getElementById("myButton");
myButton.addEventListener("click", () => {
myHeading.style.color =  RandomRGB ();                       
});

There's something about this code that doesn't make the program work. Even if I restore "myHeading.style.color = myTextInput.value; " the program doesn't run. There's something about the function, specifically about the string " rgbColor = 'rgb(' +  red + ',' +  green + ',' +  blue + ')' " that is blocking the program. Not sure what that is!

Thanks!

Sean T. Unwin
Sean T. Unwin
28,652 Points

Are you hoping for a different (random) color every button press?

Hello Sean T. Unwin, you're correct! I've updated my code down below!

Thanks for your time!

4 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Staff

Hi, Alexander D! I would be willing to bet that every time you refresh the page, it's a different color, am I correct? The reason for this is that you generate the random colors once when the JS is loaded. Every time the function is called, it runs the function again, but it does not re-run the code that was used to generate those colors.

Try placing the definitions for the red, green and blue variables inside your function just before the rgbColor definition and give it a whirl!

Hope this helps! :sparkles:

Hello Jennifer,

Thanks for your support!

The idea is to have a random color generated every time I click. With the code Guil explained, I can type any color in the text field and the H1 gets updated. So I thought I would add my random RGB function to have a random code at every click.

I saw this around before in CodePens before, but I wanted to replicate this. I'd be happy if I got any random color every time I refresh, but it's not happening.

This the updated code:

function RandomRGB () {
     var red = Math.floor(Math.random() * 256 );
     var green = Math.floor(Math.random() * 256 );
     var blue = Math.floor(Math.random() * 256 );

    var rgbColor = 'rgb(' +  red + ',' +  green + ',' +  blue + ')';
return rgbColor
}

const myHeading = document.getElementById("myHeading");
const myTextInput =  document.getElementById("myTextInput");
const myButton = document.getElementById("myButton");
myButton.addEventListener("click", () => {
myHeading.style.color = rgbColor;                       
});

I also change the very last string myHeading.style.color = rgbColor; with myHeading.style.color = RandomRGB () ; but I am not getting anywhere.

Thanks for looking after this!

Sean T. Unwin
Sean T. Unwin
28,652 Points

The function should be called, not the variable within the function, also the parentheses are missing when calling the function

myHeading.style.color = rgbColor; should be myHeading.style.color = RandomRGB(); inside the Listener.

You had this part correct in your OP, actually.

Guys,

Thanks for your support and patience! I really appreciate it, however, It's still not working.

I followed your suggestions to call the function with parenthesis as shown below. I kept the first part unchanged, hence I didn't post it here. When I click on the button, I do not generate any random color.

const myHeading = document.getElementById("myHeading");
const myTextInput =  document.getElementById("myTextInput");
const myButton = document.getElementById("myButton");

myButton.addEventListener("click", () => {
myHeading.style.color = myTextInput.value;

});

Is there anything I am doing wrong?

The interesting part is that when I replace the last part with "myHeading.style.color = myTextInput.value;" and ignore the function without referencing it all, the program doesn't work.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Staff

Alexander D At this point, we would need to see your HTML to know for sure. Also, can you confirm that you are not using Internet Explorer?` Internet Explorer does not support arrow functions. Are you receiving any errors in your console in the developer tools?

Hello Jennifer Nordell ,

The HTML is the following:

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript and the DOM</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <h1 id="myHeading">JavaScript and the DOM</h1>
    <input type="text" id="myTextInput">
    <button id="myButton">Change heading color</button>
    <p>Making a web page interactive</p>
    <script src="app.js"></script>
  </body>
</html>

There's something that I can't comprehend. This is the complete code below:

//function RandomRGB () {
//   var red = Math.floor(Math.random() * 256 );
//   var green = Math.floor(Math.random() * 256 );
//   var blue = Math.floor(Math.random() * 256 );
//
//   var rgbColor = 'rgb(' +  red + ',' +  green + ',' +  blue + ')';
//   return rgbColor
//}

const myHeading = document.getElementById("myHeading");
const myTextInput =  document.getElementById("myTextInput");
const myButton = document.getElementById("myButton");

myButton.addEventListener("click", () => {
myHeading.style.color = myTextInput.value;

});

This works fine only when the function is invisible. Once I remove the / / signs from the function and I make it visible, the program stops working. If I make the function active, and I call the function in the last line myHeading.style.color = RandomRGB (); it doesn't work.

It seems there's something about this function that is driving the program crazy.

I appreciate your patience!

Jennifer Nordell Sean T. Unwin

The code works as a charm on code pen. For some reasons the workspace is not accepting this. Thanks again for your time!