0

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>
1
  • 1
    "Instead I would like to have the array being displayed with the new item appended only" Are you trying to display only the last value added to arr? What is purpose of for..in loop? Why do you use .append()? Commented May 31, 2016 at 0:56

4 Answers 4

2

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>

Sign up to request clarification or add additional context in comments.

3 Comments

I don't think they want to modify the array to simply display an element.
Fixed it without changing original array, and updated codepen. @SpencerWieczorek
I like this answer (the changed one), most clean.
1

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>

1 Comment

Both useful answers, someone answered it quicker though. Thanks.
0

Try this

  $(function() {
      var arr = [];
      $("button").click(function() {
          arr.push($(":text").val());
          $(":text").val("");
          $("#x").append(arr.slice(-1) + '<br>');
      })
    })

Comments

0

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>

2 Comments

It's not necessarily a "better" way if the OP is only using just jQuery; also considering $.noConflict() isn't even being called in the case when you would use this when many libraries are being used.
@SpencerWieczorek better != best.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.