0

I am trying to make a simple list using JavaScript and jQuery. I am using oops concept of JavaScript. I am able to add new element in list. I am facing one problem, I have two buttons to update and delete the row. I created an array in which I have all the objects. I wanted if user click on delete button it deletes from array, as well as from the list. Same if user update the item it should also get updated in the array and list

I tried doing like this

https://jsfiddle.net/0dky3txz/3/

function Phonebook(){
    this.phoneBookArray=[];
    var defaultvalue= [{name:"abc",phonenumber:989123},{name:"pqr",phonenumber:9891462}]
    this.phoneBookArray=defaultvalue;
    console.log(this.phoneBookArray)
   }
Phonebook.prototype.addItem=function(username,phoneNumber){
    this.phoneBookArray.push({name:username,phonenumber:phoneNumber})
    this.displayItem();
}

Phonebook.prototype.displayItem=function(){
    $("#phonelist").html('');
    for(var i=0;i<this.phoneBookArray.length;i++){
        $("#phonelist").append('<li><span>'+this.phoneBookArray[i].name+'</span><span>'+       this.phoneBookArray[i].phonenumber+'</span><span><button class="del">delete</button></span><span><button class="update">update</button></span></li>')
    }
}

var phonedir=new Phonebook();
phonedir.displayItem();
$('#addItem').click(function(){

    var name=$('#nameEdit').val();
     var phone=$('#numberEdit').val();

    if(name!=''&& phone!=""){
       phonedir.addItem(name,phone);
        $('#nameEdit').val('');
        $('#numberEdit').val('');
    }else{
     alert('please enter number and name')   
    }

})


$('.del').click(function(){
    //try to delete item from list as well as from phoneBookArray
   console.log($(this).parent('li'))

})

$('.update').click(function(){
    //try to update item from list as well as from phoneBookArray

    // try to get selected item and fill on text field and update the list
   //console.log($(this).parent('li'))

})
Anshul
  • 687
  • 9
  • 18
user944513
  • 12,247
  • 49
  • 168
  • 318

0 Answers0