I want to include this code multiple times in the same page using php (include).
html code:
<div class="red" onclick="red()">red</div>
<div class="green" onclick="green()">green</div>
<div class="blue" onclick="blue()">blue</div>
<div id="change">click on the colors to change the div color</div>
css code:
.red{background-color: red;width: 50px;}
.green{background-color: green;width: 50px;}
.blue{background-color: blue;width: 50px;}
#change{background-color: yellow;width: 100px;}
javascript code:
function red()
{
document.getElementById('change').style.background="red";
}
function green()
{
document.getElementById('change').style.background="green";
}
function blue()
{
document.getElementById('change').style.background="blue";
}
This code works fine in the first div (id=change) but in the second div when I click on the div with class=red it changes the first div instead of the second.
How can I make it change the div that is below it?
problem solved:
http://jsfiddle.net/wjp4pqw6/1/
idattribute defined. The results are undefined as to what the browser should do in that situation, but as you are seeing, it typically uses the first one it finds in the HTML document.