I have this function that spawns a group of elements inside an ordered list when a button is clicked, all elements in a spawned group have the same id/timestamp. the function works fine up until an "if" statement where I validate/create/push an array of ID's. I do not understand why it will not execute.
This is my JS function, sorry about the length:
function spawnSilly() //spawn chapters function
{
const timestamp = new Date().getUTCMilliseconds();
var div = document.createElement("LI"); //creating elements
var input = document.createElement("INPUT");
var button = document.createElement("BUTTON");
var del_button = document.createElement("BUTTON")
input.setAttribute("type", "text"); //setting attributes
input.setAttribute("placeholder", "Title");
input.setAttribute("id", timestamp.toString())
button.setAttribute("type", "button");
button.setAttribute("onClick", "redirect()");
button.setAttribute("id", timestamp.toString())
button.innerHTML = "Edit";
div.setAttribute("id", timestamp.toString())
del_button.setAttribute("id", timestamp.toString())
del_button.innerHTML = "Delete Chapter";
del_button.setAttribute("onClick", "removeElement(this.id)")
div.appendChild(input) //appending to list
div.appendChild(button)
div.appendChild(del_button);
var chapterNumber = getCount(document.getElementById('spawnList'), false) //setting number of spawn
var number = $('#spawnList').children(); //fetching number of children in list
//setting chapter number to string for name attribute in input
var list = document.getElementById("spawnList")
list.insertBefore(div, list.childNodes[number]) //inserting one after another
var newSpawnNumber = string(number + 1); //setting elements class as their spawn number
input.setAttribute("class", newSpawnNumber)
button.setAttribute("class", newSpawnNumber)
div.setAttribute("class", newSpawnNumber)
del_button.setAttribute("class", newSpawnNumber)
input.setAttribute("index", newSpawnNumber)
button.setAttribute("index", newSpawnNumber)
div.setAttribute("index", newSpawnNumber)
del_button.setAttribute("index", newSpawnNumber)
if (typeof idArray !== "undefined")
{
idArray.push("#" + new Date().getUTCMilliseconds().toString())
alert("push success")
}
else if (typeof idArray == "undefined")
{
idArray = new Array()
idArray.push("#" + new Date().getUTCMilliseconds().toString())
alert("created new array, push success")
}
else
{
alert("error")
}
alert("success")
}
</script>
If I place an alert just preceding the if statement it executes, however if i place it after the if statement it does not execute, strange.
This is my HTML:
<a href="#" onclick="alert(getCount(document.getElementById('spawnList'), false));">Count Spawn</a><br/>
<form action="spawn-title-processing.php" method="post">
<ol id="spawnList">
</ol>
<button type="button" id="spawnbtn" onClick="spawnSilly();">Add </button>
<button type="button" name="spawnSubmit" onClick="submit_chpt">Save</button>
</form>
These are the console errors:
Uncaught ReferenceError: string is not defined
at spawnSilly (post_creation.php:162)
at HTMLButtonElement.onclick (VM786 post_creation.php:197)
spawnSilly @ post_creation.php:162
onclick @ VM786 post_creation.php:197
Any help would be fantastic! :)
idArrayis initialized (and that it is initialized). It's possible that it is initialized to something other than an array and so it is!== undefinedand an error is thrown when attempting topush()onto the array.var idArray = new Array()