Javascript


#1



This is an example of how to target the background element from HTML with javascript. What would be the way to target a paragraph? Thanks!


document.body.style.background = 'blue';


Javascript
Javascript
#2

@ithortureu

If I follow your syntax, it should look like this:

document.p.style.selector = value;

#3


This is some code i have. It is set up so when I click a button the background changes. I want to make it so it changes the color on It's own. Current code below( this doesnt show the button that activates it, It works )

function funcToRun() {
    var randomNumber = Math.random();
    if (randomNumber <= 0.10) {
        document.body.style.background = 'red';
    }
    else if (randomNumber <= 0.20) {
        document.body.style.background = 'blue';
    }
    else if (randomNumber <= 0.30) {
        document.body.style.background = 'lime';
    }
    else if (randomNumber <= 0.40) {
        document.body.style.background = 'fuchsia';
    }
    else if (randomNumber <= 0.50) {
        document.body.style.background = 'pink';
    }
    else if (randomNumber <= 0.60) {
        document.body.style.background = 'magenta';
    }
    else if (randomNumber <= 0.70) {
        document.body.style.background = 'orange';
    }
    else if (randomNumber <= 0.80) {
        document.body.style.background = 'aqua';
    }
    else if (randomNumber <= 0.90) {
        document.body.style.background = 'violet';
    }
    else {
        document.body.style.background = 'yellow';
    }
}


#4

This article is about changing background color after a few seconds


#5


I have made a code that auto changes color. How do I make it show on screen how many times it has changed. Code below

function funcToRun() {
  window.setTimeout( "funcToRun()", 100);
    var randomNumber = Math.random();
    if (randomNumber <= 0.10) {
        document.body.style.background = 'red';
    }
    else if (randomNumber <= 0.20) {
        document.body.style.background = 'blue';
    }
    else if (randomNumber <= 0.30) {
        document.body.style.background = 'lime';
    }
    else if (randomNumber <= 0.40) {
        document.body.style.background = 'fuchsia';
    }
    else if (randomNumber <= 0.50) {
        document.body.style.background = 'pink';
    }
    else if (randomNumber <= 0.60) {
        document.body.style.background = 'magenta';
    }
    else if (randomNumber <= 0.70) {
        document.body.style.background = 'orange';
    }
    else if (randomNumber <= 0.80) {
        document.body.style.background = 'aqua';
    }
    else if (randomNumber <= 0.90) {
        document.body.style.background = 'violet';
    }
    else {
        document.body.style.background = 'yellow';
    }
}


#6

you could create a paragraph, give it an id and use getElementById so you have a paragraph to which you can push output with innerhtml, then you just need to create a counter, and include it somewhere inside the loop. If you work outside of codecademy, you might also want to include a function call, otherwise, nothing is happening

100ms is very little, you do realize that?