Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  100] [ 1]  / answers: 1 / hits: 22828  / 9 Years ago, tue, april 21, 2015, 12:00:00

I know this is a very common error but I have read and read and can't figure it out why. It's probably something very easy but I can't solve it by myself.



var item = document.createElement(div).className = item;
var img = document.createElement(img).src = imgpath + $(this).attr(href);;
item.appendChild(img);


Any help is appreciated!



EDIT:



var item = document.createElement(div);
item.className = item;
var img = document.createElement(img);
img.src = imgpath + $(this).attr(href);
item.append(img);


This throws the same error.


More From » html

 Answers
27

In your case you are creating a div and assigns it a class name, and the same value(class name) is assigned to the item variable. So it is a string value which does not have the appendChild method.



var item = document.createElement(div);
item.className = item;
var img = document.createElement(img);
img.src = imgpath + $(this).attr(href);;
item.appendChild(img);


The same concept applies to img also


[#66983] Sunday, April 19, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
brodyfrancisi

Total Points: 1
Total Questions: 102
Total Answers: 89

Location: Marshall Islands
Member since Mon, May 31, 2021
3 Years ago
brodyfrancisi questions
;