I got it to work using my code but I don’t understand why it works. The only thing I did was add: “form id = “forFruit” " and “onsubmit =“getNewFruitAdded(); return false;”> "
I also tried to clear the list each time but the list.html(””) causes the input block to be removed. If I just use list.html() it does nothing. So two things
I was tying to figure out is why I need to do the return false to get the list to append and why the clear list does not work with my code?
why did “return false” cause the list to be appended ? Just wondering.
<!Doc Type html></Doc>
<html>
<head>
<title>Programming</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script src = "https://code.jquery.com/jquery-latest.min.js"></script>
<h1>My Favorite Fruits</h1>
<ol id = "fruitList"/>
</head>
<body>
<form id= "forFruit" onsubmit = "getNewFruitAdded(); return false;">
<input id="fruitName" placeholder="Add More Fruit" type = "text"/>
<input type = "submit"/>
</form>
<script>
var fruits = ["Apple", "Banana", "Grape","Pear"];
function redrawlist(){
var list = $("#fruitList");
list.html("")
$.each(fruits,function (index,value){
$("<li/>").text(value).appendTo(list);
})};
function getNewFruitAdded() {
newFruit = document.getElementById("fruitName").value;
fruits.push(newFruit);
console.log(fruits);
redrawlist();
}
redrawlist();