0

I am writing a simple program to list some elements from an array and then the user can input a value thats gets pushed to that array, but for some reason the array is not showing the updated array. I can see the additional values getting pushed into it but the UI is not updating? Perhaps it is the order of the code?

code:

// Data structure
const planets = [
  'Mercury', 
  'Venus', 
  'Earth'
];
    
// Loop over array
const func = (arr) => {
let items = '';
for (let i = 0; i < arr.length; i++) {
  items += `<li>${ arr[i] }</li>`;                     
}
  return items;
}


let html = `
<ul>
  ${func(planets)}
</ul>
<button id='myButton'> Add </button>
`;

document.querySelector('main').innerHTML = html;

// add to array
const addFunc = () => {
    const searchQuery = prompt('What do you want to add?');
    const search = newArray.push(searchQuery);
}

const myButton = document.getElementById('myButton');

myButton.addEventListener('click', () => {
  addFunc();                        
});
5
  • 3
    The code which updates your ui is .innerHTML = html;, which only runs once when the page loads, as it is not inside your addFunc() Commented Jun 24, 2020 at 1:38
  • Where would be a better place to put it? Commented Jun 24, 2020 at 1:46
  • 1
    you can put the code which creates the html string and performs the .innerHTML into a function of its own (eg: updateUI). You can call this function when the page loads, and also call it once you add an item to your array. Anotther way would be to leave your code as is and use .innerHTML = `<li>${ searchQuery }</li>`; as the last line in your addFunc Commented Jun 24, 2020 at 1:49
  • 1
    @Sole, Does this is what you want? codepen.io/Maniraj_Murugan/pen/ZEQKmge Commented Jun 24, 2020 at 2:00
  • @Sole, I have made it.. Commented Jun 24, 2020 at 2:25

2 Answers 2

2

try this code:

// Data structure
const planets = [
   'Mercury', 
   'Venus', 
   'Earth'
 ];
     
 // Loop over array
 const func = (arr) => {
 let items = '';
 for (let i = 0; i < arr.length; i++) {
   items += `<li>${ arr[i] }</li>`;                     
 }
   return items;
 }
 

 // add to array
 const addFunc = () => {
   const searchQuery = prompt('What do you want to add?');
   planets.push(searchQuery)

   displayHTML()
 }
 
const displayHTML = () => {
   let html = `
   <ul>
     ${func(planets)}
   </ul>
   <button id='myButton' onclick='addFunc()'> Add </button>
   `;
   
   document.querySelector('main').innerHTML = html;
}

displayHTML()

 const myButton = document.getElementById('myButton');
 
 myButton.addEventListener('click', () => {
   addFunc();                        
 });
Sign up to request clarification or add additional context in comments.

3 Comments

Although thats nearly there, It only runs the click once
you need to remove ` <button id='myButton'> Add </button> ` from js and add it to html
or you can replace <button id='myButton'> Add </button> with <button id='myButton' onclick="addFunc()"> Add </button> in javascript
1

Make a common function like getHtml(planets) and passdown the array you have on page load.

And this function will generate ul , li via func(planets) and button

And also place the eventlistener here for the add button.

Also modify your addFunc like,

const addFunc = () => {
    const searchQuery = prompt('What do you want to add?');
    planets.push(searchQuery);
    getHtml(planets)
}

Note: You have used newArray in your code but you have not declared that elsewhere but trying to push an element which will trow error.

Working Snippet as follows,

// Data structure
const planets = [
  'Mercury', 
  'Venus', 
  'Earth'
];
    
// Loop over array
const func = (arr) => {
let items = '';
for (let i = 0; i < arr.length; i++) {
  items += `<li>${ arr[i] }</li>`;                     
}
  return items;
}

const getHtml = (planets) =>{
  let html = `
<ul>
  ${func(planets)}
</ul>
<button id='myButton'> Add </button>
`;

  document.querySelector('main').innerHTML = html;

 const myButton = document.getElementById('myButton');
  
  myButton.addEventListener('click', () => {
    addFunc();                        
  });
}

getHtml(planets)

// add to array
const addFunc = () => {
    const searchQuery = prompt('What do you want to add?');
    planets.push(searchQuery);
    getHtml(planets)
}
<main> </main>

2 Comments

Thanks, newArray was left in by mistake
@Sole, Okay just keep that point in future questions.. Happy coding..

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.