Color changes with JavaScript

4

Hi, I'm learning JavaScript and I have a problem with the following exercise within my html I have my ID which have the name red and blue colors.

and within my JS I have my next line of code.

const redButton = document.getElementById('redButton');
const blueButton = document.getElementById('blueButton');

redButton.addEventListener('click', (e) => {
  const colorSquare = document.getElementById('colorDiv');
  colorSquare.style.backgroundColor = 'red';
});

blueButton.addEventListener('click', (e) => {
  colorSquare.style.backgroundColor = 'blue';
})

What I want is that when you click click on the network button, the ID colorDiv change color to red and when you do click on the blue button change the color to blue.

I think that the way to do this is with conditional if and else , but I do not know how to declare it for this event to take place.

    
asked by Polo Moreno 12.01.2017 в 01:44
source

2 answers

6

You could not see the changes because your div did not have any size.

Code:

const redButton = document.getElementById('redButton');
const blueButton = document.getElementById('blueButton');
const colorSquare = document.getElementById('colorDiv');
redButton.addEventListener('click', (e) => {
    colorSquare.style.backgroundColor = 'red';
});
blueButton.addEventListener('click', (e) => {
    colorSquare.style.backgroundColor = 'blue';
});
#colorDiv{
    width:10px;
    height:10px;
}
<button type="button" id="redButton">Click Me!</button>
<button type="button" id="blueButton">Click Me!</button>
<div id="colorDiv"></div>

Now about your doubt:

  

Could this also be solved with conditional if and else ?

Note that in this example you need a variable to be able to comply with the conditions of if and else .

To test the example, you must modify the value of the variable n , and you will see color changes.

var n = 30;//cambia el valor y veras cambios en el color del div

if (n >= 60)
{
    document.getElementById("miDiv").style.backgroundColor = 'red';
}
else  
if (n >= 41 && n <50)  
{
    document.getElementById("miDiv").style.backgroundColor = 'black';
}  
else  
if (n <=40)  
{
    document.getElementById("miDiv").style.backgroundColor = 'blue';
}
#miDiv{
    width:100px;
    height:100px;
}
<div id="miDiv">
</div>
    
answered by 12.01.2017 / 02:02
source
1

As you very well told @ x-rw, what your code lacks to see the change of background color of your div is to assign a size.

However, I would like to give you a recommendation: do not change the styles directly with the attribute style of Javascript, if not using classes failing, which you can also change from Javascript.

Why not do it with the style attribute?

Because if you use:

colorSquare.style.backgroundColor = 'blue';

What you're really doing is this:

<div id="colorDiv" style="background-color: blue"></div>

According to CSS specificity , Inline styles added to an element (eg, style="font-weight: bold") always replace any style written in external style sheets , or what is the same, if you have a class or an ID overwriting This style, you will not be able to, since the inline styles are those that have a greater specificity.

Therefore, I recommend you use classes in their default and change them with Javascript.

  • To add a class to an element:

    colorSquare.classList.add("rojo");
    
  • To remove a class from an element:

    colorSquare.classList.remove("rojo");
    

Your modified example:

const redButton = document.getElementById('redButton');
const blueButton = document.getElementById('blueButton');
const colorSquare = document.getElementById('colorDiv');
redButton.addEventListener('click', (e) => {
    colorSquare.classList.remove("azul");
    colorSquare.classList.add("rojo");
});
blueButton.addEventListener('click', (e) => {
    colorSquare.classList.remove("rojo");
    colorSquare.classList.add("azul");
});
#colorDiv{
   width:10px;
   height:10px;
}

.azul{
   background-color: blue;
}

.rojo{
   background-color: red;
}
<button type="button" id="redButton">Click Me!</button>
<button type="button" id="blueButton">Click Me!</button>
<div id="colorDiv"></div>
    
answered by 16.11.2017 в 13:11