After a few hours of trial and error I put my post here with a question, maybe one of you will be able to help me. I've created a function that is called with the "onclick" attribute after clicking on the button. This function changes font size in selected elements of the page. I would like to add a different percentage, e.g. 200% at second click on the button. I'd appreciate any help.
JS code:
document.getElementsByClassName("increase-size").addEventListener("click", increasSize);
function increasSize() {
var x = document.querySelectorAll("h1, h2, h3, h4, h5, h6, p, input, a");
var i;
for (i = 0; i < x.length; i++) {
x[i].style.fontSize = "150%";
}
}
HTML code:
<button type="button" class="increase-size" onclick="increasSize()"></button>
I tried to solve it with a counter, but I failed.
document.getElementsByClassName("increase-size").addEventListener("click", increaseSize);
var counter = 0;
function increaseSize() {
counter += 1;
var x = document.querySelector("html");
var i;
for (i = 0; i < x.length; i++) {
if(counter == 1) {
x[i].style.fontSize = "150%";
} else if (counter == 2) {
x[i].style.fontSize = "200%";
}
}
}
counter++at the end of your function. Also, please note your function is misnamedincreasSizeinstead ofincreaseSize("e" before "Size"). Works fine but it's good to avoid typos like that to easily reference the function somewhere else without having to dig back to the source.