Image doesn't display inside innerHTML
up vote
2
down vote
favorite
I'm wanting to get an image to display using innerHTML and I just can't seem to get it to work. It works fine with text and I'm trying to get it to display dynamically with other items inside innerHTML. Updated: the image is inside another element in the innerHTML. Image Example:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.innerHTML = '<a href="/"><h3>title</h3>'+ image + '</a>';
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/<div id="block">
image
</div>javascript html
add a comment |
up vote
2
down vote
favorite
I'm wanting to get an image to display using innerHTML and I just can't seem to get it to work. It works fine with text and I'm trying to get it to display dynamically with other items inside innerHTML. Updated: the image is inside another element in the innerHTML. Image Example:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.innerHTML = '<a href="/"><h3>title</h3>'+ image + '</a>';
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/<div id="block">
image
</div>javascript html
1
innerHTMLtakes a string, not anHTMLElement
– Heretic Monkey
Nov 12 at 20:01
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I'm wanting to get an image to display using innerHTML and I just can't seem to get it to work. It works fine with text and I'm trying to get it to display dynamically with other items inside innerHTML. Updated: the image is inside another element in the innerHTML. Image Example:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.innerHTML = '<a href="/"><h3>title</h3>'+ image + '</a>';
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/<div id="block">
image
</div>javascript html
I'm wanting to get an image to display using innerHTML and I just can't seem to get it to work. It works fine with text and I'm trying to get it to display dynamically with other items inside innerHTML. Updated: the image is inside another element in the innerHTML. Image Example:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.innerHTML = '<a href="/"><h3>title</h3>'+ image + '</a>';
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/<div id="block">
image
</div> var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.innerHTML = '<a href="/"><h3>title</h3>'+ image + '</a>';
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/<div id="block">
image
</div> var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.innerHTML = '<a href="/"><h3>title</h3>'+ image + '</a>';
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/<div id="block">
image
</div>javascript html
javascript html
edited Nov 12 at 20:08
asked Nov 12 at 19:58
K.C.
1798
1798
1
innerHTMLtakes a string, not anHTMLElement
– Heretic Monkey
Nov 12 at 20:01
add a comment |
1
innerHTMLtakes a string, not anHTMLElement
– Heretic Monkey
Nov 12 at 20:01
1
1
innerHTML takes a string, not an HTMLElement– Heretic Monkey
Nov 12 at 20:01
innerHTML takes a string, not an HTMLElement– Heretic Monkey
Nov 12 at 20:01
add a comment |
6 Answers
6
active
oldest
votes
up vote
1
down vote
accepted
The recommended way of adding an image would be via appendChild(), however to add an image element in the way you require, you'd need to specify the image in terms of its HTML tag and attributes as html, as follows:
var div = document.createElement('div');
// Define the image tag and html directly and then apply to innerHTML in this way
div.innerHTML = '<img src="' + "https://picsum.photos/200/300" + '" />';
document.getElementById('block').appendChild(div);<div id="block">
image
</div>add a comment |
up vote
2
down vote
innerHtml is used to parse a string as HTML code.
To attach the image to the DOM element use:
div.appendChild(image);
add a comment |
up vote
1
down vote
Use .appendChild() on the image to make it a child of the div, instead of innerHTML:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.appendChild(image);
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/<div id="block">
image
</div>See "innerHTML += ..." vs "appendChild(txtNode)" for the differences in the two
add a comment |
up vote
1
down vote
Use this code instead of innerHTML
var div = document.createElement('div');
var image = document.createElement("img");
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
document.getElementById('block').appendChild(div).appendChild(image);
add a comment |
up vote
0
down vote
you need to use appendChild instead of innerHTML:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");
div.appendChild(image);
document.getElementById('block').appendChild(div);<div id="block">
image
</div>add a comment |
up vote
0
down vote
You could change this line
div.innerHTML = image;
To this:
div.innerHTML = image.outerHTML;
You're setting the innerHTML to an object currently instead of to html.
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
The recommended way of adding an image would be via appendChild(), however to add an image element in the way you require, you'd need to specify the image in terms of its HTML tag and attributes as html, as follows:
var div = document.createElement('div');
// Define the image tag and html directly and then apply to innerHTML in this way
div.innerHTML = '<img src="' + "https://picsum.photos/200/300" + '" />';
document.getElementById('block').appendChild(div);<div id="block">
image
</div>add a comment |
up vote
1
down vote
accepted
The recommended way of adding an image would be via appendChild(), however to add an image element in the way you require, you'd need to specify the image in terms of its HTML tag and attributes as html, as follows:
var div = document.createElement('div');
// Define the image tag and html directly and then apply to innerHTML in this way
div.innerHTML = '<img src="' + "https://picsum.photos/200/300" + '" />';
document.getElementById('block').appendChild(div);<div id="block">
image
</div>add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
The recommended way of adding an image would be via appendChild(), however to add an image element in the way you require, you'd need to specify the image in terms of its HTML tag and attributes as html, as follows:
var div = document.createElement('div');
// Define the image tag and html directly and then apply to innerHTML in this way
div.innerHTML = '<img src="' + "https://picsum.photos/200/300" + '" />';
document.getElementById('block').appendChild(div);<div id="block">
image
</div>The recommended way of adding an image would be via appendChild(), however to add an image element in the way you require, you'd need to specify the image in terms of its HTML tag and attributes as html, as follows:
var div = document.createElement('div');
// Define the image tag and html directly and then apply to innerHTML in this way
div.innerHTML = '<img src="' + "https://picsum.photos/200/300" + '" />';
document.getElementById('block').appendChild(div);<div id="block">
image
</div> var div = document.createElement('div');
// Define the image tag and html directly and then apply to innerHTML in this way
div.innerHTML = '<img src="' + "https://picsum.photos/200/300" + '" />';
document.getElementById('block').appendChild(div);<div id="block">
image
</div> var div = document.createElement('div');
// Define the image tag and html directly and then apply to innerHTML in this way
div.innerHTML = '<img src="' + "https://picsum.photos/200/300" + '" />';
document.getElementById('block').appendChild(div);<div id="block">
image
</div>answered Nov 12 at 20:02
Dacre Denny
8,1254729
8,1254729
add a comment |
add a comment |
up vote
2
down vote
innerHtml is used to parse a string as HTML code.
To attach the image to the DOM element use:
div.appendChild(image);
add a comment |
up vote
2
down vote
innerHtml is used to parse a string as HTML code.
To attach the image to the DOM element use:
div.appendChild(image);
add a comment |
up vote
2
down vote
up vote
2
down vote
innerHtml is used to parse a string as HTML code.
To attach the image to the DOM element use:
div.appendChild(image);
innerHtml is used to parse a string as HTML code.
To attach the image to the DOM element use:
div.appendChild(image);
answered Nov 12 at 20:01
rufus1530
331110
331110
add a comment |
add a comment |
up vote
1
down vote
Use .appendChild() on the image to make it a child of the div, instead of innerHTML:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.appendChild(image);
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/<div id="block">
image
</div>See "innerHTML += ..." vs "appendChild(txtNode)" for the differences in the two
add a comment |
up vote
1
down vote
Use .appendChild() on the image to make it a child of the div, instead of innerHTML:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.appendChild(image);
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/<div id="block">
image
</div>See "innerHTML += ..." vs "appendChild(txtNode)" for the differences in the two
add a comment |
up vote
1
down vote
up vote
1
down vote
Use .appendChild() on the image to make it a child of the div, instead of innerHTML:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.appendChild(image);
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/<div id="block">
image
</div>See "innerHTML += ..." vs "appendChild(txtNode)" for the differences in the two
Use .appendChild() on the image to make it a child of the div, instead of innerHTML:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.appendChild(image);
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/<div id="block">
image
</div>See "innerHTML += ..." vs "appendChild(txtNode)" for the differences in the two
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.appendChild(image);
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/<div id="block">
image
</div>var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
div.appendChild(image);
document.getElementById('block').appendChild(div);/*work fine if appendChild(image) or text inside innerHTML*/<div id="block">
image
</div>answered Nov 12 at 20:01
j08691
164k20188212
164k20188212
add a comment |
add a comment |
up vote
1
down vote
Use this code instead of innerHTML
var div = document.createElement('div');
var image = document.createElement("img");
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
document.getElementById('block').appendChild(div).appendChild(image);
add a comment |
up vote
1
down vote
Use this code instead of innerHTML
var div = document.createElement('div');
var image = document.createElement("img");
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
document.getElementById('block').appendChild(div).appendChild(image);
add a comment |
up vote
1
down vote
up vote
1
down vote
Use this code instead of innerHTML
var div = document.createElement('div');
var image = document.createElement("img");
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
document.getElementById('block').appendChild(div).appendChild(image);
Use this code instead of innerHTML
var div = document.createElement('div');
var image = document.createElement("img");
image.setAttribute('src', "https://picsum.photos/200/300");/*sample pic*/
document.getElementById('block').appendChild(div).appendChild(image);
answered Nov 12 at 20:09
sonysadi
113
113
add a comment |
add a comment |
up vote
0
down vote
you need to use appendChild instead of innerHTML:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");
div.appendChild(image);
document.getElementById('block').appendChild(div);<div id="block">
image
</div>add a comment |
up vote
0
down vote
you need to use appendChild instead of innerHTML:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");
div.appendChild(image);
document.getElementById('block').appendChild(div);<div id="block">
image
</div>add a comment |
up vote
0
down vote
up vote
0
down vote
you need to use appendChild instead of innerHTML:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");
div.appendChild(image);
document.getElementById('block').appendChild(div);<div id="block">
image
</div>you need to use appendChild instead of innerHTML:
var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");
div.appendChild(image);
document.getElementById('block').appendChild(div);<div id="block">
image
</div> var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");
div.appendChild(image);
document.getElementById('block').appendChild(div);<div id="block">
image
</div> var div = document.createElement('div');
var image = document.createElement('img');
image.setAttribute('src', "https://picsum.photos/200/300");
div.appendChild(image);
document.getElementById('block').appendChild(div);<div id="block">
image
</div>answered Nov 12 at 20:02
Andreas
443
443
add a comment |
add a comment |
up vote
0
down vote
You could change this line
div.innerHTML = image;
To this:
div.innerHTML = image.outerHTML;
You're setting the innerHTML to an object currently instead of to html.
add a comment |
up vote
0
down vote
You could change this line
div.innerHTML = image;
To this:
div.innerHTML = image.outerHTML;
You're setting the innerHTML to an object currently instead of to html.
add a comment |
up vote
0
down vote
up vote
0
down vote
You could change this line
div.innerHTML = image;
To this:
div.innerHTML = image.outerHTML;
You're setting the innerHTML to an object currently instead of to html.
You could change this line
div.innerHTML = image;
To this:
div.innerHTML = image.outerHTML;
You're setting the innerHTML to an object currently instead of to html.
answered Nov 12 at 20:04
CamJohnson26
559725
559725
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269247%2fimage-doesnt-display-inside-innerhtml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
innerHTMLtakes a string, not anHTMLElement– Heretic Monkey
Nov 12 at 20:01