4

I realize that this has been asked a million times... but sorting through all the ones I've found, I haven't found one that really explains it well.

HTML:

<div id="alphabet"></div>

JS:

var alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", 
                "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];

var target = document.getElementById('alphabet');

for (i = 0; i < 26; i++) {

    var newLink = document.createElement('div');
    target.appendChild = newLink;
    newLink.innerHTML = alphabet[i];
}

alert(alphabet);
alert(newLink);
alert(target);

Obviously there is something I'm missing... With such a simple example I can't believe I'm having this much trouble. Any help is much appreciated, thanks in advance!

1 Answer 1

2

Basically node.appendChild(node) is a function.

target.appendChild(newLink);

And your full code would be,

for (i = 0; i < 26; i++) {
 var newLink = document.createElement('div');
 newLink.innerHTML = alphabet[i];
 target.appendChild(newLink);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your complete and swift response. I (hilariously) just made the opposite mistake with the .innerHTML not being a function. It is working as intended now. I will mark your answer as accepted once the timer allows me to.
@perkface Glad to help! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.