i have a problem with simple if else statement that changes text color of a
element depending on a current color. I see why i get certain results but i can't seem to find a solution. Any help to newbie like me appreciated.
HTML <p id="demo">JavaScript can change the style of an HTML element.</p>
JS TRY 1
<script>
var x = document.getElementById("demo");
function one() {
x.style.fontSize="16px";
x.style.color="black";}
function two() {
x.style.fontSize="16px";
x.style.color="red";}
function three(){
if(x.style.color="black") { two() }
else {one()
}
}
</script>` <button type="button" onclick="three()">Click Me!</button>
JS TRY 2
<script>
var brojilo = 1 ;
function three(){
var x = document.getElementById("demo");
function two() {
x.style.fontSize="18px";
x.style.color="red";}
function one() {
x.style.fontSize="18px";
x.style.color="black";}
if (brojilo % 2 == 0)
{one()}
else
{two()}
var brojilo = brojilo + 1 ;
}
</script><button type="button" onclick="three()">Click Me!</button>
JS TRY 3
<script>
var b2 = 0 ;
function brojanje(){
var b2 = b2+1;}
function three(){
var x = document.getElementById("demo");
function two() {
x.style.fontSize="18px";
x.style.color="red";}
vfunction one() {
x.style.fontSize="18px";
x.style.color="black";}
if (b2 % 2 == 0)
{one()}
else
{two()}
}
</script><button type="button" onclick="three(); brojanje ();">Click Me!</button>
Thanks in advance
Aucun commentaire:
Enregistrer un commentaire