I have a literal array of image IDs and I need to swap them in <img src="">
to Next or Previous image on buttons click events. The initial current image ID is known from the img src
provided server-side on initial page load.
Obviously, before swapping, the URL needs to be constructed with the target ID like this:
'http://site.com/images/' + imageID + '.jpg'
I'm a JS/jQuery beginner and would like to learn a correct, minimalistic approach. TIA.
My code to start off:
var images=["777777","666666","555555"];
var max = $(images).length;
$('#swapnextimg').click{
$("#imageswap").attr('src', ...... );
}
<a id="swapnextimg"></a>
<a id="swapprevsimg"></a>
<div id="imagebox">
<img id="imageswap" src="http://site.com/images/123456.jpg">
</div>
Try below code,
I assume that you've got one img tag, and only one. And that the purpose of this is to change that single image tag.
To do that you will need the nex and prev button.
The object:
now for the next and prev to work. We are going to take a look at the
.on()
method.Hope this helped you. If not please let me know
Here is an example:
http://jsfiddle.net/ndFGL/