0

When I'm trying to push new name in to my array, enter key don't respond and there for I can't add another names.

Part of the JS:

$("input[type='text']").keypress(function(event){
 if(event.which === 13){
  var newName = $(this).val();
   $("#" + "newName").append("#" + names);
   names.push(this.value);
   }
});

My Codepen

2 Answers2

0

You could try something like that:

var names = [
    "Nadav",
    "Yaniv",
    "Golan",
    "Asaf",
    "Boaz",
    "Moshe"
];

$("input[type='text']").keypress(function(event){
    if(event.which === 13) {
        names.push(this.value);
    }
});

If you type "something" in your input field, then press Return key, you names array have now a new item:

var names = [
    "Nadav",
    "Yaniv",
    "Golan",
    "Asaf",
    "Boaz",
    "Moshe",
    "something"
];
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
0

Here is the easiest way to add it to the array, based on your code:

var names = [


 "Nadav",
  "Yaniv",
  "Golan",
  "Asaf",
  "Boaz",
  "Moshe"
]

$("input[type='text']").keypress(function(event){
    if(event.which === 13){
        var newName = $(this).val();
        $("#" + "newName").append("#" + names); //WHAT ARE YOU DOING HERE EXACTLY?
        names.push(newName); //THIS IS THE DIFFERENT LINE 
    }
});

That will add the name to the names array. However, what exactly are you doing with that line that I've commented on?

Brandon Dixon
  • 1,036
  • 9
  • 16