I'm trying to display images that are sent from the controller to the view. On the view page, I want to display the image, and I have 2 buttons "forth" and "back" to iterate over these images.
//controller:
var images= [path1 , path2, path3];
var currentImageIndex=0;
function goToNextImage() { // is called when forth button is clicked
if (currentImageIndex +1 < images.length) {
currentImageIndex ++;
imageDisplay= ImageDisplay(images, currentImageIndex);
}
}
$(document).ready(function() {
imageDisplay= ImageDisplay(images, currentImageIndex);
$('body').append(imageDisplay);
});
// the body of my index file is empty. I append everything to the body from the javascript file
// view (javascript file)
function ImageDisplay(images,currentImageIndex) {
var me = $('<div/>');
drawImage();
function drawImage( ) {
var windowWidth= $(window).width();
var windowHeight=$(window).height();
var imageSrc= images[currentImageIndex];
var image = $ ('<img id="img" src="'+imageSrc+'" alt="NO IMAGE" width="'+windowWidth/2+ '" height= "'+windowHeight/2+'" class="image_display"/>');
console.log("image source= "+imageSrc);
me.append(image);
}
return me ;
}
The problem is that the displayed image is always the same (Although the image source changes).
I read here Change image source with javascript that I should use document.getElementById("img").src
to set the new source.
But as I don't have any img id in my html file, I got this error "cannot set property src of null".
Thank you for help!