Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Pieter Bracke
4,078 PointsUncaught TypeError: eToggle.appendChild is not a function
Hi, I am having an issue with dynamically adding buttons to my document using javascript. (I know jQuery is alot easier but I also want to learn to do it with js so bear with me :p).
Basically I think my code works up untill my last line then I get the follwing error : Uncaught TypeError: eToggle.appendChild is not a function...
This is the jfiddle link: https://jsfiddle.net/6620Lcfv/17/
and this is all the code for those donn't want to fiddle :p
function seperate(){
var eContainer = document.createElement('div'); //Create element div and safe in variable eContainer
var eKnop = document.createElement('button'); //Create element button and safe in variable eKnop
eKnop.appendChild(document.createTextNode("Verberg afbeelding")); //Set innerHTML for eKnop
eContainer.appendChild(eKnop); //append eKnop in eContainer
var eToggle = document.getElementsByClassName("toggle"); //append eContainer in eToggle
eToggle.appendChild(eContainer); //append eContainer in eToggle
}
<div class="toggle"><img class="screenshot" width="238" height="222" src="../images/clip_image014.gif" alt="schermafdruk"> </div>
Basically what I want to do is create a new button and add this into a new created div and then append this created div together with the button in an already existing div with class of toggle.... When I try to append to the body instead of this already existing div it works fine but that's not the outcome I want any idea what is the solution to this error?
EDIT: var eToggle = document.getElementsByClassName("toggle")[0]; //append eContainer in eToggle
Needed to add the [0] at the end of my line!
1 Answer
Nejc Vukovic
Full Stack JavaScript Techdegree Graduate 51,574 PointsHi. I saw that you found the problem :D
Here is how I would do it:
function seperate() {
var eToggle = document.getElementById('toggleID'); //append eContainer in eToggle
var eContainer = document.createElement('div'); //Create element div and safe in variable eContainer
var eKnop = document.createElement('button'); //Create element button and safe in variable eKnop
eKnop.innerHTML = "Verberg afbeelding"; //you can access DOM Objects keys with dot notation no need for calling a function
eContainer.appendChild(eKnop); //append eKnop in eContainer
eToggle.appendChild(eContainer);
}
seperate();
IMO it's better to work with id="" than class when appending dynamic DOM - because you can never be sure that the element you are looking for is on the index position [0].
Just a thought ;)
Best, Nejc