0

Here is popup.html file :

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <div id="mList"></div>
    <script src="util.js"></script>
</body>
</html>

And here is util.js :

var options = ['','Second Option','Third Option'];

function makeUL(array) {
var list = document.createElement('ul');

/*
chrome.storage.sync.get(null, function(items) {
        var allKeys = Object.keys(items);
        allKeys.forEach(function(key) {
            console.log(key);
            options.push(key);
        });
   });
 */


for(var i = 0; i < array.length; i++) {
    var item = document.createElement('li');

    item.appendChild(document.createTextNode(array[i]));

    list.appendChild(item);
}

return list;
 }

 document.getElementById('mList').appendChild(makeUL(options));

Whenever I try to create the list dynamically the portion of the code commented, it doesn't work. It just gives me an empty HTML. But if I keep the code what it is currently, it works fine.

How this problem can be resolved ?

Here is full project link.

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
  • Possible duplicate of [saving and retrieving from chrome.storage.sync](https://stackoverflow.com/questions/14531102/saving-and-retrieving-from-chrome-storage-sync) – Noel Schenk Aug 19 '19 at 15:24
  • @NoelSchenk can you explain how this question is duplicate of the question you have marked? – Maifee Ul Asad Aug 19 '19 at 15:50
  • As far as I can see the problem lays inside your get function. Explained [here](https://developer.chrome.com/apps/storage) or in the question marked. `chrome.storage.sync.get(['key'], function(result) { console.log('Value currently is ' + result.key); });` – Noel Schenk Aug 19 '19 at 16:11
  • The problem isn't about retrieving `keys` or it's `values`. The problem is - it is not creating the list when I try to create list by `chrome.storage.sync.get`, but it works fine when the values in my array `options` is pre-defined. – Maifee Ul Asad Aug 19 '19 at 16:17
  • And my commented code works just fine, I have tested it few seconds ago by console logging. – Maifee Ul Asad Aug 19 '19 at 16:18
  • Since the API is asynchronous the correct duplicate is [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](//stackoverflow.com/q/23667086) – wOxxOm Aug 19 '19 at 16:43

0 Answers0