JavaScript How do I make this array into a list using getElementById and innerHTML? make this array,make array python,make array golang,make

I need to make the array 'places' display its contents in the list. My problem is my function. I know I'm not doing it right. Can anyone help? Do I need to use getElementsByTagName?

<body>
      <header>
        <h1>
           Hands-on Project 3-3
        </h1>
      </header>

      <article>
        <h2>Scouting Locations</h2>
        <div id="results">
            <ul>
               <li id="item1"></li>
               <li id="item2"></li>
               <li id="item3"></li>
               <li id="item4"></li>
               <li id="item5"></li>
            </ul>
        </div>
      </article>
      <script>
   
   	  var places = ["Philadelphia", "New York", "Los Angles", "Austin", "Kansas City"];
   	
   	  function processPlaces() {
   		  for (i = 0, i < 5, i++) {
   			  var listItem = "item" + (i + 1);
   			  document.getElementById(listItem).innerHTML = places[i]; 
   			
   		  }
   	  }
   	
   	  if (window.addEventListener) {
   		window.addEventListener("load", processPlaces, false);
   	  } else if (window.attachEvent) {
   		window.attachEvent("onload", processPlaces);
   	  }
   	
      </script>
      </body>
Answer:1

you could grab the containing div results and creating a ul like this var locations = $('<ul>') and then creating a function that loops through you array and within each iteration creates a li and it's content then appends it to the ul, and finally at the end appends the ul to the container div. Here is a rough version of what that might look like in jQuery. Same concept in JS slightly different syntax.

var results = $('#results');
var list = $('<ul>');

function processPlaces() {
      for (i = 0; i < 5; i++) {
          var listItem = $('<li>');
          listItem.text(places[i]); 
          list.append(listItem);
      }
     results.append(list);
  }

this dynamic approach may be better so that as you add stuff to the array you do not need to go back to the html and manually add more li's each time.

Answer:2

I am testing emit and listen methods with VueJS to learn how to use it. I get some strange results, I don't understand. I would expect my function initMap() is called and the console is logging, which ...

I am testing emit and listen methods with VueJS to learn how to use it. I get some strange results, I don't understand. I would expect my function initMap() is called and the console is logging, which ...

  1. vuejs event handler
  2. vuejs event handler parameters
  3. vuejs event handler component
  4. vuejs event listener
  5. vuejs event listener on component

I have been unable to find any examples of RSA-OAEP encryption in IE11. Here's a snippet of my implementation, in which I am given a very vague error. function convertStringToArrayBufferView(str)...

I have been unable to find any examples of RSA-OAEP encryption in IE11. Here's a snippet of my implementation, in which I am given a very vague error. function convertStringToArrayBufferView(str)...

I was wondering if it is possible to override "undefined" for uninitiated keys of an object such that: var obj={} console.log(obj.randomKey) output in console: 0 I am well aware that I "forgot" to ...

I was wondering if it is possible to override "undefined" for uninitiated keys of an object such that: var obj={} console.log(obj.randomKey) output in console: 0 I am well aware that I "forgot" to ...

I want to create 4 <span> tags and add different text to them. for (var i = 0; i <= 4; i++) { var spanElement = document.createElement("span") + i; var value_of_span += uniques + i; ...

I want to create 4 <span> tags and add different text to them. for (var i = 0; i <= 4; i++) { var spanElement = document.createElement("span") + i; var value_of_span += uniques + i; ...

  1. create multiple tags javascript
  2. create multiple tags in wordpress
  3. create multiple tags
  4. git create multiple tags
  5. docker create multiple tags
  6. sharepoint create multiple tags
  7. aws cli create multiple tags
  8. sublime text create multiple tags