Hello Digital Point Forums, I have been reading a book on JavaScript the last few weeks and I really wanted to create something but the script I have created below does not work. I do not really understand why it does not work. It works when I enter first username... it changes the innerHTML of paragraph chan... but when I go to change the username once more it displays "You have changed your name from undefined to W3Geek". The pPlayerName is initialized and I even put an alert message to check and see if it is stored... it says "W3G has been stored" or something like that. The code is below, please help me with it. Thank you, W3Geek <html lang="en"> <head> <title>Username Change</title> <script type="text/javascript"> var changeName = 0; function userName() { var playerName = document.getElementById("name").value; if (playerName != "" && changeName == 0) { document.getElementById("chan").innerHTML = "Your player name is " + playerName; } if (playerName != "" && changeName >= 1) { alert("You have changed your name from " + pPlayerName + " to " + playerName + "!"); document.getElementById("chan").innerHTML = "Your player name is " + playerName; } if (changeName == 0) { var pPlayerName = document.getElementById("name").value; alert(pPlayerName + " was saved!"); } changeName++; //each time this function is ran, changeName goes increases by one. } </script> </head> <body> <form name="error" action="" method=""> <input type="text" id="name" name="name" value="" onchange="userName();" /> </form> <p id="chan" name="chan"></p> </body> </html> Code (markup):
Your "pPlayerName" variable can't have been initialized, not on the page that you've provided in your post. Note that if you're initializing it in another <iframe>, that won't be known any other <iframe>.