0

I am trying to display the the data in an Unordered list which is coming from an API. Below is the code how I have implemented it. Its working completely fine and the data gets displayed as well. But as soon as someone clicks on any other buttons the data disappears as if the list is getting destroyed.

<div class="vendorMarketScroll">
     <ul id="vendorMarketList">
     </ul>
</div>

Populating the List (vendorMarket is containing the data from the API):

function makeVendorMarketList(vendorMarket) {
            if (vendorMarket && vendorMarket.length > 0)
            {
                vendorMarket.sort(function (a, b) {
                return (a.marketName < b.marketName) ? -1 : (a.marketName > b.marketName) ? 1 : 0;
                }); 
                for (var market in vendorMarket) {
                    if(vendorMarket[market].marketName){
                        var z = document.createElement('li');
                        z.innerHTML = vendorMarket[market].marketName;
                        document.getElementById("vendorMarketList").appendChild(z);
                    }
                }
            }  
        }

Please help me in understanding the reason behind this, and what is the correct way to implement this?

Ajay Kushwaha
  • 80
  • 1
  • 10
  • Could you provide some code related to the button that cause the problem? – Saeed Shamloo Dec 05 '21 at 19:11
  • Its not a button as such, its the click event or I believe any even I guess. eg: I check/uncheck a checkbox or click on some other button that page only. Its more like the list is getting destroyed. – Ajay Kushwaha Dec 05 '21 at 19:26
  • I tested your code next to other checkbox and button elements and didn't see any problem. – Saeed Shamloo Dec 05 '21 at 19:35
  • Thanks for checking and taking out time, the page is build in aspx, don't have in depth knowledge about them. Could that be one of the reason? – Ajay Kushwaha Dec 06 '21 at 07:11

1 Answers1

0

The Problem was something different: the page was getting loaded for many places and hence the creation function had to be called from all the places.

It was kinda my mistake, I was new to the solution.

Thanks to all those who invested their time and tried helping me:)

Ajay Kushwaha
  • 80
  • 1
  • 10