mercredi 6 juillet 2016

jQuery add to Array and output result


I am creating a feature where users can click on li's and it will add it to an array. I need to know that this is working and wanted to output the values held in the array.

I don't have much experience of this so need some help.

This is my code so far:

$.noConflict();
jQuery( document ).ready(function( $ ) {
  var itemArr = [];

  $('.item').click(function() {
    var itemid = $(this).attr("id");
    var dataid = $(this).data('id');
    var idx = $.inArray(dataid, itemArr);

    if (idx >= 0) {
      itemArr.splice(idx, 1);
      $("#"+itemid).removeClass('green');
    } else {
      itemArr.push(dataid);
      $("#"+itemid).addClass('green');
    }

    $.each(itemArr, function(index, value){
      $( "#success" ).append( document.createTextNode(" - " + value));
    });
  });

The problem is when I try to show the array results in success it says "undefined" so now I'm not sure if it's undefined in the array or just on output.

Your help is appreciated.


Aucun commentaire:

Enregistrer un commentaire