When i run my code and try the Delete button i get an error message. When you type the name of a game it is supposed to delete that game from the list and update that list. I've checked both the value of the box and the array location. Both are intact and correct but for some reason the error is saying it still cannot be found. I've console logged it and checked if it was a comparison issue. in the delete game function I've tried two ways which are still listed. Any suggestions?
var GameArray = [];
window.onscroll = function() {myfunction()};
function readTextFile(file)
{
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var allText = rawFile.responseText;
GameArray = allText.split("\n");
}
}
}
rawFile.send(null);
}
readTextFile("MoreInfo/ListOfGames.txt");
for(var x = 0;x < GameArray.length; x++){
GameArray[x] = GameArray[x].replace(/-/g,' ');
}
callGames();
function callGames(){
GameArray.sort();
for(var x = 0;x < GameArray.length; x++){
var checkbox = document.createElement("INPUT");
checkbox.setAttribute("type","checkbox");
document.getElementById("games").innerHTML += GameArray[x];
document.getElementById("games").appendChild(checkbox);
document.getElementById("games").innerHTML += '<br />';
}
}
function exitButton(){
window.close();
}
function addGame(){
var addNewGame = document.getElementById("myText").value;
GameArray.push(addNewGame);
document.getElementById("games").innerHTML = "";
callGames();
}
function deleteGame(){
var addNewGame = document.getElementById("myText").value;
var count = 0;
if (GameArray.indexOf(addNewGame) > -1){
console.log("Found it!");
}
else {
console.log("Error");
}
for(var x = 0;x<GameArray.length;x++){
if(GameArray[x] == addNewGame){
if(x == 0){
count++;
GameArray.splice(0,1);
document.getElementById("games").innerHTML = "";
callGames();
}
else{
count++;
GameArray.splice(x,x);
document.getElementById("games").innerHTML = "";
callGames();
}
}
}
if(count == 0){
alert("I'm sorry but the game you have listed has not been found, Please try again.");
}
}
<div align="right" id="sticky" class="header">
<h1 id="welcomeUser">Welcome </h1>
<button id="add" onclick="addGame()">Add</button>
<input type="text" id="myText" placeholder="Enter Game Name Here" name="textBoxName">
<button id="delete" onclick="deleteGame()">Delete</button>
<button id="exitbutton" onclick="exitButton()">EXIT</button>
</div>
<div><p align="center", id=games></p></div>
<form action="scripts/saved.html", id=formValues>
<h1>Warning pushing submit wont allow any more changes to be made</h1>
<input type="submit" value="Submit", id=submitButtonCSS>
</form>