1

I have a static array, that I loop through to display three columns.

Boys, Girls and All.

So far I have added a looping method that displays all the elements in the array as such:

  boys.forEach(function(boy) {
  strboy += '<li>'+ boy + '</li>';
  }); 

 strboy += '</ul>';
 document.getElementById("boys").innerHTML = strboy;

this works fine, the issue comes, when I want the user to add another element to the array. I have made a function as such

function addboy(){
var boy=document.getElementById('newboy').value
  boys.push(boy);
  console.log(boys);
}

this works, and the new array is logged to the console, but how can I update the UI, after the user added a new element. I tried not using an anonymous function, but then the entire new array got displayed again, I only want the new array to be displayed. I would prefer this with just Vanilla JavaScript and without JQuery.

3
  • 2
    Wrap your first code in a function and run that function again to update the UI. Commented Sep 5, 2018 at 13:51
  • Are you looking for observables? Commented Sep 5, 2018 at 13:51
  • The problem with this is that the entire array is displayed once again, not just the new array. Even if I call the function upon window.onload(). Commented Sep 5, 2018 at 13:52

3 Answers 3

1

Put your code in a function an simply call it every time you add a new boy

boys = ['Mario', 'Pepe']
function updateUi() {
  strboy = '<ul>';
  boys.forEach(function(boy) {
    strboy += '<li>' + boy + '</li>';
  });

  strboy += '</ul>';
  document.getElementById("boys").innerHTML = strboy;
}


function addboy() {
  var boy = 'Jhon Doe'
  boys.push(boy);
  updateUi()
}

updateUi()
<div id="boys"> </div>
<button onclick="addboy()">add boy</button>


Without redrawing the array ->

boys = ['Mario', 'Pepe']
function updateUi() {
  strboy = '<ul id="boysUl">';
  boys.forEach(function(boy) {
    strboy += '<li>' + boy + '</li>';
  });

  strboy += '</ul>';
  document.getElementById("boys").innerHTML = strboy;
}


function addboy() {
  var boy = 'Jhon Doe'
  boys.push(boy);
  document.getElementById("boysUl").innerHTML += '<li>' + boy + '</li>';
}

updateUi()
<div id="boys"> </div>
<button onclick="addboy()">add boy</button>

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

1 Comment

@baileyhaldwin this method gonna help you : Element.insertAdjacentHTML() perfect to append a piece of HTML where you want. developer.mozilla.org/en-US/docs/Web/API/Element/…
1

Using ES6

/**
 * Initialize the boys list, using given boys
 */
function init(initialBoys) {
  const str = initialBoys.reduce((tmp, x) => `${tmp} <li>${x}</li>`, '');

  document.getElementById('boys').innerHTML = str;
}

/**
 * Generate a random number between 0 and max
 */
function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}

/**
 * Add a new boy in the list, pick up a boy from a predefined list
 * Pick it up randomly
 */
function addRandomNewBoy() {
  const availableBoys = [
    'Christian',
    'Amir',
    'Julian',
    'Rorelio',
    'Juan',
    'Grégory',
    'Santa claus',
    'Little Mouse',
    'Your mum',
  ];

  addBoy(availableBoys[getRandomInt(availableBoys.length - 1)]);
}

/**
 * Add a boy to the list
 */
function addBoy(boy) {
  let str = document.getElementById('boys').innerHTML;

  str = `${str} <li>${boy}</li>`;

  document.getElementById('boys').innerHTML = str;
}

/**
 * launch the initialization of the boys list
 */
init([
  'David',
  'Eliot',
]);
<ul id='boys'>
</ul>

<button onclick="addRandomNewBoy()">Add a good boi</button>

Comments

0

You have not to redraw all. Just add an element! So, without jQuery, you can write(if 'boys' is ul id)

function addBoy(boy){
    let currentBoys = document.getElementById("boys").innerHTML;
    document.getElementById("boys").innerHTML = currentBoys + '<li>' + boy + '</li>';
}

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.