I'm a student and what I'm trying to do here is to build a table from Javascript using arrays stored in the Javascript. The table works well enough but I can't seem to integrate the Images into the table. No matter what I try the ImgArray remains undefined. Could any of you point me in the right direction of getting this to work?
var pet = [{
species: 'fruit bat',
name: 'bats',
colour: 'grey',
size: 'small',
food: 'apples',
limb: "wings",
img: "0"
}, {
species: 'goat',
name: 'bastard',
colour: 'off white',
size: 'goat-sized',
food: 'clothing',
limb: "hooves",
img: "1"
}, {
species: 'butterfly',
name: 'flutterby',
colour: 'rainbow',
size: 'petite',
food: 'nectar',
limb: "wings",
img: "2"
}, {
species: 'buzzard',
name: 'Buzz',
colour: 'molted black and white',
size: 'bigish',
food: 'carrion',
limb: "wings",
img: "3"
}, {
species: 'pixie',
name: 'petty',
colour: 'blue',
size: 'tiny',
food: 'souls',
limb: "wings",
img: "4"
}, {
species: 'tortoise',
name: 'Tank',
colour: 'Green',
size: 'smoothbacked',
food: 'lettuce',
limb: "legs",
img: "5"
}];
//array for holding images for the pet array
imgArray = [];
imgArray[1] = new Image();
imgArray[1].src = "resources/bat.gif"; //source of the image
imgArray[2] = new Image();
imgArray[2].src = "resources/goatVec01.png";
imgArray[3] = new Image();
imgArray[3].src = "resources/butterfly.gif";
imgArray[4] = new Image();
imgArray[4].src = "resources/buzzard01.png";
imgArray[5] = new Image();
imgArray[5].src = "resources/breezie01.png";
imgArray[6] = new Image();
imgArray[6].src = "resources/turtle.gif";
var len = imgArray.length;
function display() { //added a for loop
var i;
for (i = 0; i < len; ++i) {
var pic = document.write(imgArray[i].outerHTML);
}
}
function makeTr(pet, attrName) {
var html = '',
i;
html += "<tr>";
for (i = 0; i < pet.length; ++i) {
if (attrName = "img") {
document.getElementById("pic".outerHTML)
html += "<td>" + display() + "</td>";
} else {
html += "<td>" + pet[i][attrName] + "</td>";
}
html += "</tr>";
return html;
}
}
function buildTable(pet) {
var html = '';
html += "<table border='4px'>";
html += "<caption>Pets Avaliable</caption>";
html += makeTr(pet, "species");
html += makeTr(pet, "name");
html += makeTr(pet, "colour");
html += makeTr(pet, "size");
html += "</table>";
document.getElementById("work").innerHTML = html;
}
<!DOCTYPE html>
<html>
<head>
<!-- Used to declare the character for use in Firefox. http://stackoverflow.com/questions/11996257/the-character-encoding-of-the-html-document-was-not-declared-->
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"></meta>
<meta content="utf-8" http-equiv="encoding"></meta>
<title>Pets</title>
<script type="text/javascript" src="java01.js">
</script>
<body>
<button id="please" onclick="buildTable(pet)">Work</button>
<p id="work"></p>
</body>
</html>