简体   繁体   中英

Replace div contents javascript (no jquery)

Every time a selection is made from a dropdown menu, specific data is pulled from facebook and added to different divs. I am trying to update the contents of the div every time a different selection is made, however at the minute, the contents are just appended on after the initial contents.

This is the code that gets data based on a selection and creates the list from the returned data

   <script> 

    city = document.getElementById("citySelection")
    city.addEventListener("change", function() {
      var selected = this.value;
      var eventsList = document.getElementById("events");
      if (selected == "None") {
        eventsList.style.display = "none";
      } else {
        eventsList.style.display = "block";
      };
      if (selected == 'Bristol') {
        getBristolEvents();
      };
      if (selected == 'Leeds') {
        getLeedsEvents();
      };
      if (selected == 'Manchester') {
        getManchesterEvents();
      };
      if (selected == 'Newcastle') {
        getNewcastleEvents();
      };
    });


    function createList(response, listId) {
      var list = document.createElement('UL')
      for (var i = 0; i < 10; i++) {
        var events = response.data[i].name
        var node = document.createElement('LI');
        var textNode = document.createTextNode(events);
        node.appendChild(textNode);
        list.appendChild(node)
        listId.appendChild(list);
     }};

   </script

This is the div being targeted:

<html>
 <div id="events" style="display: none">
     <div id="eventsDiv"  style="display: block">
      <div id="eventsListOne">
        <h3 id='headerOne'></h3>
     </div>
      <div id="eventsListTwo">
        <h3 id='headerTwo'></h3>
      </div>
      <div id="eventsListThree">
        <h3 id='headerThree'></h3>
      </div>
    </div>
  </div>
 </div>
</html>

I have tried resetting the innerHtml of the div every time the function to get the data from facebook is called:

   <script>
    function getEventsThree(fbUrl, title) {
     var listId = document.getElementById('eventsListThree');
     var headerThree = document.getElementById('headerThree');
     listId.innerHtml = "";
     headerThree.append(title)
     FB.api(
       fbUrl,
       'GET', {
         access_token
       },
       function(response) {
         listId.innerHtml = createList(response, listId)
       }
     )};
   </script>

However, that still doesn't reset the contents of the div.

I've looked at other response but they all use jquery which I am not using.

Can anyone advise on the best way to fix this? Thanks.

I think your Hennessy approach is fine. Generate the inner content, then set .innerHTML .

At least one of your problems, maybe the only one, appears to be that you set .innerHTML to the return value of createList , but that function does not return anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM