I have the following array:
function theMiddle(){
var arrayTheMiddle = new Array (showName.theMiddle +"<br />" + beginingTime.theMiddle +" <br />" + network.abc +"<br />" + duration.thirty +"<br />" + rating.general +"<br />" + description.theMiddle +"<br />" + showImagetheMiddle);
document.getElementById("gridbar").innerHTML=(arrayTheMiddle);
}
The last object in that array, showImagetheMiddle is an image with the following code:
var showImagetheMiddle = new Image();
showImagetheMiddle.src = 'abc.jpg';
All my objects appear on my webpage but the showImagetheMiddle gives me the following error:
[object HTMLImageElement]
I’ve looked around but found more complex ways to display an image, I would like to keep it simple and add the image within my existing array (as shown above). Is this possible? If not what am I doing wrong here?
Any help would be most appreciated!
Since
arrayTheMiddleis the array, you need to use the subscript operator[]to get an element from it. In your example, the image is the first (0-th) element in the array. Your line should be this instead:As a side note, it’s much better to create arrays using the square bracket notation
[](not the subscript operator).EDIT: After seeing your comment I now recommend you use
.appendChildto insert the element into the document. Because simply appending by+won’t work the way you expect. Here is your code:I found some misconceptions in your code. For example, I don’t think you understand how arrays work. So here is some information:
Syntax:
Array elements are created using the square bracket notation
[]. The elements of an array are separated by the comma operator,. For instance:Array elements are separated only by commas. The
+operator doesn’t separate them. So here is valid code, but there is only one element in the array:In your code example, arrays are superfluous. You can very well do what you need without it. So here is your new code: