1

Messed up with a basics.

As I press the enter key or click the button, list array's element is changed, not appended the new element I wrote.

Also, I would like to reset for <input type='text' maxlength='8' id='aName' placeholder='Write a name.' onKeyDown='enterkey();' />, but have no clue to do.

How can I amend the code below?

function enterkey() {
  if (window.event.keyCode == 13) {
    pushingName();
  }
}

function pushingName(){
  var list=[];
  var oneName = document.getElementById('aName').value;
  var stringName = oneName.toString();

  var nameList = list.push(stringName);

  console.log(list);
}
<input type='text' maxlength='8' id='aName' placeholder='Write a name.' onKeyDown='enterkey();' />
<button onclick='pushingName();'>ADD</button>

2
  • @CalvinNunes Thanks. and how can I reset input value? Commented Feb 7, 2020 at 14:10
  • I deleted my first comment and since I saw other things that I think is relevant to you (I presume you are learning), I added an answer below, take a look, if it is useful for you, upvote and accept, please. Commented Feb 7, 2020 at 14:19

1 Answer 1

1

You can try this updated snippet below.

in addition if you need it, it also:

  1. cuts spaces at the beginning and end so ' ss ' => 'ss'

  2. does nothing when string is empty

  3. avoiding duplicates in a array

  4. resets input value after it was added. (not resets in cas of attempting to addd a dupicate)

  5. removes using another button

let list = [];


function enterkey() {
  if (window.event.keyCode == 13) {
    pushingName();
  }
}

function pushingName(){

  var elName = document.getElementById('aName');
  var stringName = elName.value.toString();
  
  // trimming string if you need it
  stringName = stringName.trim();
  
  if (!stringName.length) {
    return; // check ot mpty string if you need it
  }

  if (list.indexOf(stringName) === -1) { // avoid duplications if you need it
    var nameList = list.push(stringName);
    
    elName.value = ''; // reset value to empty if was added
  }
  
  console.log(list);
}

function removeName(){

  var oneName = document.getElementById('aName').value;
  var stringName = oneName.toString();
  
  // trimming string if you need it
  stringName = stringName.trim();
  
  if (!stringName.length) {
    return; // check ot mpty string if you need it
  }

  // filtering our stringName
  list = list.filter(item => item !== stringName);

  
  console.log(list);
}
<input type='text' maxlength='8' id='aName' placeholder='Write a name.' onKeyDown='enterkey();' />
<button onclick='pushingName();'>ADD</button>
<button onclick='removeName();'>REMOVE</button>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.