The idea of the program is to make a loop that will iterate over the option elements in the select box and only add even numbers to it (from 16 to 30 including 30). To be specific : It is a fontSizeBox and I want to add font sizes to it so the user can choose from them .
The problem is that I get numbers starting from 16 to 23 and not as expected . The code :
window.onload = function() {
let fontSizeBox = document.createElement("select");
fontSizeBox.name = "fontSize-box";
document.body.prepend(fontSizeBox);
let fontSizeBoxLabel = document.createElement("label");
fontSizeBoxLabel.appendChild(document.createTextNode("Choose the font size :"));
fontSizeBox.before(fontSizeBoxLabel);
for (let i = 16; i <= 30; i += 2) {
fontSizeBox.appendChild(document.createElement("option"));
}
Array.from(fontSizeBox);
/* The above code will make the fontSizeBox an array so I can iterate over
it using the loop */
for (let i = 0; i <= 7; i++) {
fontSizeBox[i].innerHTML = i + 16;
/* The following code won't work without the number constructor
because I tried this code in the console and here is the output:
typeof fontSizeBox[0].innerHTML;
>> 'string' */
if ((Number(fontSizeBox[i].innerHTML) % 2) !== 0) {
continue;
}
}
}
<title>Document</title>
var element = document.createElement("option"); element.innerHTML = i; fontSizeBox.appendChild(element);And remove all the code below that.Array.from(fontSizeBox)does not convert thefontSizeBoxto an array. It returns an array which you need to assign to another variable:var fontSizeBoxArray = Array.from(fontSizeBox)