Question

Link

This kind of thing is exactly that I have, and the push and splice is working marvellously. But I want to add(push) items when I click on some sort of button.
Like: Button click... data.items.push(anything)... and it appears in the list (the array which I am using like a list). Link

Was it helpful?

Solution

Enter move and type into the input boxes and then click the button, this will add the movie and type and unique ID in the object.

var data = {items: [
    {id: "1", name: "Snatch", type: "crime"}
]};

$('button').on('click',function(){
    var name = $('#name').val();
    var type = $('#type').val();
    var id = parseInt(data.items[data.items.length-1].id)+1;

    data.items.push({"id":id.toString(), "name":name,"type":type});
    console.log(data);
});

DEMO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top