I am learning MVC using vanilla Javascript. I am trying to load an image when a button is pressed, but I believe my syntax is off. Any ideas?
here are my scripts. I think the problem is in the View script, at the document.getElementById line.
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script src="Controller.js"></script>
<script src="View.js"></script>
<script src="Model.js"></script>
<title></title>
</head>
<body onload="buildImgArr()">
<div align="center">
<img id="imageFrame" >
</div>
<div align="center">
<button id="loadImage" type="button" onclick="controlIMG()" >Click to load image</button>
</div>
</body>
</html>
control script
function controlIMG(){
var randImg = randomImg();
loadIMG(randImg);
}
Model script
var myImages = new Array(2);
function buildImgArr(){
//alert("In Array");
myImages[1] = "hiragana/A.PNG";
myImages[2] = "hiragana/CHI.PNG";
}
function randomImg(){
//alert("in model");
var rand = myImages[Math.floor(Math.random() * myImages.length)];
return rand;
}
View script
function loadIMG(imgsrc){
alert("In view " + imgsrc);
//text-align:center; margin: 0px auto; display:block
document.getElementById("imageFrame").innerHTML = '<img src="imgsrc">';
}
innerHTML = '<img src="imgsrc">';? Is the variable name mysteriously extracted from the string to a live URL? +imgcan't have HTML, you need to put the image into another element, like div or sth. Or alternatively just setsrcof the existing image ...