lundi 1 mai 2017

If statement for when an array has no value (Javascript + HTML)

I have an input box storing various texts into an array and it is displaying on another page, but I want the page to say "No Events" when there is no value in the array

here is the javascript that appends the array to the page

function getEvents(){

//get the localstorage from the new event page
events = JSON.parse(localStorage.getItem("events"));

for (var i = 0; i < events.length; i++)
{

    if (events === "undefined" || null || "") {
        document.getElementById("none").innerHTML = "No events";        
    } else {
    // title output
    var title = document.createElement("h1"); //creates h1 element
    var titleText = document.createTextNode(events[i].name); //assigns title 
text
    title.appendChild(titleText); //appends text to h1
    document.getElementById("output").appendChild(title); //appends to the 
page

    // date output
    var date = document.createElement("p"); //creates p element
    var dateText = document.createTextNode(events[i].date);  //assigns date 
text
    date.appendChild(dateText); //appends text to p
    document.getElementById("output").appendChild(date); //appends to the page
    }
}
}

And here is the html on the event loader page

<body onload="getEvents()">

    <!-- Title -->
    <center>
        <h1>My Events</h1>
    </center>

    <p id="none"></p>
    <div id="output">
    </div>
</body>

Aucun commentaire:

Enregistrer un commentaire