When I try to loop through my array, the result is the entire array being displayed each time the button is clicked (doubling up). Instead I would like to have the array being displayed with the new item appended only.
Thanks :^)
<!DOCTYPE HTML>
<html>
<head>
<title>Checklist</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script>
$(function() {
var arr = [];
$("button").click(function() {
arr.push($(":text").val());
$(":text").val("");
for(i in arr){
$("#x").append(arr[i] + "<br>");
}
//x.text(JSON.stringify(arr));
})
})
</script>
</head>
<body>
<input type="text" id="item" placeholder="Enter an item">
<button id="add">Add Item</button>
<br>
<div id="x"></div>
</body>
</html>
Why don't you get the last element of the Array, and then append it, instead of using this for...in loop:
$(function() { var arr = []; $("button").click(function() { arr.push($(":text").val()); $(":text").val(""); $("#x").append(arr[arr.length - 1] + "<br>"); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="item" placeholder="Enter an item"> <button id="add">Add Item</button> <br> <div id="x"></div>
Call $("#x").empty()
before appending value to #x
; substitute for
loop for for..in
loop
<!DOCTYPE HTML> <html> <head> <title>Checklist</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> <script> $(function() { var arr = []; $("button").click(function() { arr.push($(":text").val()); $(":text").val(""); $("#x").empty(); for(var i = 0; i < arr.length; i++){ $("#x").append(arr[i] + "<br>"); } //x.text(JSON.stringify(arr)); }) }) </script> </head> <body> <input type="text" id="item" placeholder="Enter an item"> <button id="add">Add Item</button> <br> <div id="x"></div> </body> </html>
alternatively, you can use arr.length -1
<!DOCTYPE HTML> <html> <head> <title>Checklist</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> <script> $(function() { var arr = []; $("button").click(function() { arr.push($(":text").val()); $(":text").val(""); $("#x").append(arr[arr.length - 1] + "<br>"); }) }) </script> </head> <body> <input type="text" id="item" placeholder="Enter an item"> <button id="add">Add Item</button> <br> <div id="x"></div> </body> </html>
Try this
$(function() {
var arr = [];
$("button").click(function() {
arr.push($(":text").val());
$(":text").val("");
$("#x").append(arr.slice(-1) + '<br>');
})
})
the new item appended only
but keep the array
jQuery(function($) { // a better way to DOM ready var arr = [], item = $("#item"), x = $("#x"), y = $("#y"); // Cache elements $("button").click(function() { var val = item.val(); // get the input value arr.push(val); // OK, you appended to array item.val(""); // Reset input x.append(val + "<br>"); // the new item appended only y.text(JSON.stringify(arr)); // And test our array }); });
#y{background: #eee;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="item" placeholder="Enter an item"> <button id="add">Add Item</button> <br> <div id="x"></div> <div id="y"></div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.