-3

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

Community
  • 1
  • 1
InVaDeR
  • 3
  • 1
  • 5

1 Answers1

0

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

Anton
  • 32,245
  • 5
  • 44
  • 54
  • sorry, but its not DOING anything. Can u also display it please. also add the comment at the starting of the part of the code where it is just displaying, 'cause i have that figured out. – InVaDeR Jul 31 '13 at 11:48
  • @user2152309 Look in the console everytime you click the button and you will see the object will be added in the array – Anton Jul 31 '13 at 11:50
  • by console u mean where it is displaying code? because i'm not getting anything! and i want to display it in the screen because the array i am using is like... a playlist – InVaDeR Jul 31 '13 at 12:00
  • @user2152309 so you want something like this? http://jsfiddle.net/XYVaA/ – Anton Jul 31 '13 at 12:04
  • yes yes yes just i want to add things to list by the click of a button. and for the next time pls tell me firest that u are working on my answer, i dont really have patience i need to see a therapist!!! – InVaDeR Jul 31 '13 at 12:08
  • @user2152309 check this fiddle out http://jsfiddle.net/XYVaA/2/ – Anton Jul 31 '13 at 12:11
  • The id is not increasing after 1 addition! – InVaDeR Jul 31 '13 at 12:16
  • You click the arrow up on the left side of the answer and click the check mark under the arrow, 16 is not old :) – Anton Jul 31 '13 at 12:18
  • @user2152309 i've updated the fiddle reopen it with the /2 at the end. – Anton Jul 31 '13 at 12:19
  • good thing that you have used push. Just one more thing, as i said i want to use it as a playlist, im combining it with this:http://www.beyondhyper.com/jplayer/. This guy also uses jso but when i asked him how to add songs, he didn't take interest. You think my thing will work with his playlist? – InVaDeR Jul 31 '13 at 12:30
  • I guess so, if you save the things you added to a server. – Anton Jul 31 '13 at 12:36
  • It will work if you make more adjustments for the site – Anton Jul 31 '13 at 12:39
  • huh? what? oh god why i have to write long, repute me so i can chat! – InVaDeR Jul 31 '13 at 12:40
  • Well if you want to have the things you've added to show next time you enter the website you need to save the array to sql or text file etc. – Anton Jul 31 '13 at 12:42
  • oooohhh. i thought that. Can you teach me pls? im not boring you am i? – InVaDeR Jul 31 '13 at 12:43
  • You'll need to create another question on SO :/ i've got to go soon – Anton Jul 31 '13 at 12:43
  • k you have been of gr8 help bye! when i post that question, is there any way to notify you? – InVaDeR Jul 31 '13 at 12:46
  • 'for (var i = 0; i < data.items.length;i++){' what does that mean? – InVaDeR Jul 31 '13 at 12:48
  • why to use .push? what does that change? – InVaDeR Jul 31 '13 at 13:46