I want to get two html buttons of the class fontsizer. I need to create a variable, store the buttons in the variable, and then loop through the variable and assign each of the two buttons an event handler for onclick. In the end, when a button is clicked, it should run a method named resizeText.
Here is my JavaScript code:
function startup() {
var fontButtons = document.getElementsByClassName("fontsizer");
var i;
alert(fontButtons.length);
for (i = 0; i < fontButtons.length; i++) {
fontButtons.elements[i].onclick = resizeText(this);
}
function resizeText(objButton) {
var fontChange;
fontChange = parseFloat(objButton.value);
if (document.body.style.fontSize == "") {
document.body.style.fontSize = "1.0em";
}
var currentFontSize;
alert("changed");
currentFontSize = parseFloat(document.body.style.fontSize);
currentFontSize = currentFontSize+fontChange;
document.body.style.fontSize = "currentFontSize+em"
}
From my browser, the resizeText function is not run. When I check the size of fontButtons before looping, it is 0. Where am I going wrong?