Oluderi
Oluderi

Reputation: 323

Delete button next to each array's value

I have a HTML-JavaScript script in which the user can insert data to a new array [] by using a form's text field and an insert button.

By pressing insert button, the user inserts the data typed into the array.
I have a function which prints all the values of the array into <p id="demo"></p> and runs itself every 100 milliseconds in order to be updated with the arrays values.
I also have a reset button to delete every array's value when clicked.

What I want to do is add a delete button next to each array's value in order to be easier for the user to delete the wrong value he inserted.

I am using this code to insert values and print them:

HTML:

<div align="center">
    <form id="form1">
        <input type="text" name="fname" id="fname" placeholder="Type here!">
    </form>
    <br>
    <input type="button" id="Button Insert" onclick="myFunction()" value="Insert">
    <input type="button" onclick="myFunction3()" value="Reset">
</div>
<p id="demo" align="center"></p>

JavaScript/JQuery:

var all_values =[];

function myFunction() {
    var temp_val = $("#fname").val();
    all_values.push(temp_val);
    document.getElementById("form1").reset();
}

setInterval(function () {
    $("#demo").html(all_values.join("<br>"));
}, 100);

function myFunction3() {
    all_values.length = 0; 
}

To be more specific I want something like these things: iOS example JSFiddle Example 1 JSFiddle Example 2.

Could you please help me? Thanks in advance.

Upvotes: 2

Views: 1947

Answers (2)

Artur Filipiak
Artur Filipiak

Reputation: 9157

I'd do it the other way around.

Remove setInterval as it's really bad way to do such things.
Remove white spaces from the id attribute (id="Button-Insert", not id="Button Insert")
Don't use onclick attributes. Instead, register click event handlers with jQuery

// caching is a good practice when you reffer to the same elements multiple times:
var all_values =[], demo = $("#demo"), form = $("#form1")[0], fname = $("#fname");

$('#Button-insert').click(function(){
    var temp_val = fname.val();
    all_values.push(temp_val);
    // create delete button along with the value
    demo.append('<p>'+temp_val+' <button value="'+temp_val+'" type="button" class="del-btn">Delete</button></p>');
    form.reset();
});

$('#Button-reset').click(function(){
    all_values = []; 
    demo.html('');
});

// event delegation for dynamic elements:
demo.on('click', '.del-btn', function(){
    all_values.splice(all_values.indexOf($(this).val()), 1);
    $(this).parent().remove();
});

JSFiddle

Upvotes: 2

oscargilfc
oscargilfc

Reputation: 339

Simply create the delete buttons at the same time you create the table.

 function loadvalues(){
  var i, button; 
  $('#demo').empty();
  for(i in all_values){
      $('#demo').append(all_values[i]);
      button = $('<button>',{'text':'Delete'}).click(function(){
           all_values.splice(this,1);
           loadvalues();
      }.bind(i)).appendTo('#demo');
      $('#demo').append('<br>');
   }
 }

Also you don't need to poll, you could simply add each one on demand with a function like this:

 function addVal(){
     var val = $("#fname").val(), i = all_values.length;
     all_values.push(val);

     $('#demo').append(val);
     button = $('<button>',{'text':'Delete'}).click(function(){
            all_values.splice(this,1);
            loadvalues();
    }.bind(i)).appendTo('#demo');
    $('#demo').append('<br>');
 }

I had some typos, the code works, Check here: http://codepen.io/anon/pen/QbvgpW

Upvotes: 0

Related Questions