0

I have a html-file, looks like:

...
<div class="colorbox" data-product="oldUpload" data-color="brown" style="background-color: #31261d" title="Brown"></div>
<div class="colorbox" data-product="oldUpload" data-color="cranberry" style="background-color: #6e0a25" title="Cranberry"></div>
...
<div class="colorbox" data-product="TSHIRT" data-color="brown" style="background-color: #31261d" title="Brown"></div>
<div class="colorbox" data-product="TSHIRT" data-color="cranberry" style="background-color: #6e0a25" title="Cranberry"></div>
...
<script src="profiles.js"></script>

And following JavaScript file:

function getSelectedColors() {
  let colorboxes = document.getElementsByClassName('colorbox');
  let selectedColors = [];
  for (let colorbox of colorboxes) {
    if (colorbox.classList.contains('checked')) {
        selectedColors[colorbox.dataset.product] = (selectedColors[colorbox.dataset.product] || '') + colorbox.dataset.color + ',';
    }
  }
  console.log('Colors:' + selectedColors);
  console.log(selectedColors);
  return selectedColors;
}

If I run the function getSelectedColors() the output in console is:

Line 1: "Colors: "

Line 2: "[oldUpload: "brown,cranberry,pink,purple,", TSHIRT: "cranberry,...]"

So it seems, the code in function is asynchronous, because "selectedColors" is an empty array directly after for-loop and the function also returns an empty array. But at the moment, I don't understand why, because I think, there's nothing asynchronous in my code.

So why this JS code is behaving asynchronous?

Thanks, Klaus

8
  • Are you running this in Chrome? The Chrome console is fairly notorious for doing weird things like this. Commented May 1, 2019 at 17:05
  • @Pointy: Yeah, that was just an error in writing my question. I just fixed it. Commented May 1, 2019 at 17:07
  • @MaheerAli: The script tag is already at the end. I will edit my question to clarify this. Commented May 1, 2019 at 17:07
  • 5
    No, your code is not asynchronous. ""selectedColors" is an empty array directly after for-loop" - what makes you think that? Though yes, of course, the array will still have a .length of 0. What the console shows you are non-index properties of the array object. Don't abuse arrays like that! Commented May 1, 2019 at 17:08
  • @Eric yes, th doc should be definitly ready, beause the javascript file is included at the end Commented May 1, 2019 at 17:11

2 Answers 2

2

Change let selectedColors = []; to let selectedColors = {};

In JS spec arrays only have a numeric index. Other indices are not enumerated and so "invisible" in your console.

Sign up to request clarification or add additional context in comments.

Comments

0
  'Colors:' + selectedColors

Through concatenating the array with a string, the array gets implicitly converted to a string. That will join all the values of the array:

 "" + [1, 2, 3] // "1,2,3"

In your case the array is actually empty, it contains no numerical keys.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.