Javascript slideshow. Src of images in another directory

145 Views Asked by At

I'm new in Java script and I'm trying to build a slideshow for my page. Here is my html code:

<div id="sildeshow">    
        <img src="Imagini\CEJ2016\img3.jpg" id="imag">
        <div id="stanga"><img onclick='slideSHOW(-1)' class="buton_stanga" src="Imagini\left.png"></div>
    <div id="dreapta"><img onclick='slideSHOW(1)' class="buton_dreapta" src="Imagini\right.png"></div>
</div>

and my Js script:

<script type="text/javascript">
    var numar_imagine=1;
    var maxim=14;

    function slideSHOW(x){
        var imagine=document.getElementById('imag');
        numar_imagine +=x;
        if(numar_imagine>maxim){
            numar_imagine=1;
        }
        if(numar_imagine<0){
            numar_imagine=maxim;
        }
        imagine.src="Imagini\CEJ2016\img" + numar_imagine + ".jpg";
    }
</script>

Now, the thing is that for some reason my img source does not change properly when I press my left/right arrows.

Trying to solve the problem I noticed that if my images are in same directory with my .html file the code works.

Any advice? Thanks.

4

There are 4 best solutions below

1
On BEST ANSWER

\ is escape char. your solution is below

function slideSHOW(x){
    var imagine=document.getElementById('imag');
    numar_imagine +=x;
    if(numar_imagine>maxim){
        numar_imagine=1;
    }
    if(numar_imagine<0){
        numar_imagine=maxim;
    }
   imagine.src="Imagini\\CEJ2016\\img" + numar_imagine + ".jpg";
}
0
On

You need to escape the backslash in your string for your source in your JavaScript

imagine.src="Imagini\\CEJ2016\\img" + numar_imagine + ".jpg";

0
On

Please try this:

imagine.src="Imagini\\CEJ2016\\img" + numar_imagine + ".jpg";
1
On

Why are you using \ instead of / in the first place?

this solves the problem:

imagine.src="Imagini/CEJ2016/img" + numar_imagine + ".jpg";

Or maybe try adding extra escape \ as other answers says.