I'm new to Javascript. I'm creating a cell phone animation to illustrate probability. I have a graphic of an iPhone and buttons for all the keys. When a button is pressed the corresponding number shows on the phone screen (box I drew and ID as "phone"). However, there are some stipulations: for example, the 3-digit area code and 3-digit prefix cannot start with 1 (etc.). So I have conditions that must be met before the text can be appended. Here is my code for entering the number 1 when its button is pressed:
var phone = document.getElementById("phone");
if(phone.value.length == 0) {
phone.innerHTML = phone.innerHTML + "";
}
else if(phone.value.length == 6) {
phone.innerHTML = phone.innerHTML + "";
}
else if(phone.value.length == 3) {
phone.innerHTML = phone.innerHTML + "1)";
}
else if(phone.value.length == 8) {
phone.innerHTML = phone.innerHTML + "1-";
}
else if(phone.value.length == 2) {
phone.innerHTML = phone.innerHTML + "";
}
else {
phone.innerHTML = phone.innerHTML + "1";
}
Unfortunately, this code doesn't work. If I comment out all the code except something like
var phone = document.getElementById("phone");
phone.innerHTML = phone.innerHTML + "1)";
then it works. So I am assuming I don't have the correct syntax for the conditionals. Thanks!
phoneis an<input>element and has avaluethen using itsinnerHTMLdoesn't make much sensephone? it it's an input just change all .innerHTML with .value.valueandinnerHTMLon a single node doesn't add up. Nor does a huge series ofif-else(if)conditions. Look intoswitch(which "falls through" in JS), don't add'', justbreakin those cases, and set up a fiddle