Forum Moderators: open
<img src="error.jpg" id="foo" data-images="1.jpg,2.jpg,3.jpg" data-image-index="1" />
(function () {
var foo = document.getElementById('foo'),
images = foo.getAttribute('data-images'),
image-index = foo.getAttribute('data-image-index'),
n;
if (images) {
images = images.split(",");
image-index = (image-index? parseInt(image-index, 10): 0);
n = images.length;
function nextImage() {
image-index = (image-index + 1 < n? image-index + 1: 0);
foo.src = images[image-index];
}
nextImage();
foo.onclick = nextImage;
}
})();
(function () {
var foo = document.getElementById('foo'),
images = foo.getAttribute('data-images'),
imageindex = foo.getAttribute('data-image-index'),
n;
if (images) {
images = images.split(",");
imageindex = (imageindex? parseInt(imageindex, 10): 0);
n = images.length;
function nextImage() {
imageindex = (imageindex + 1 < n? imageindex + 1: 0);
foo.src = images[imageindex];
}
nextImage();
foo.onclick = nextImage;
}
})();
(function () {
var foo = document.getElementById('foo'),
details = document.getElementById('details'),
images = foo.getAttribute('data-images'),
imageindex = foo.getAttribute('data-image-index'),
n;
if (images) {
images = images.split(",");
imageindex = (imageindex? parseInt(imageindex, 10): 0);
n = images.length;
function setImg(el, src) {
el.src = src;
details.value = src;
}
function nextImage() {
imageindex = (imageindex + 1 < n? imageindex + 1: 0);
setImg(foo, images[imageindex]);
}
setImg(foo, images[imageindex]);
foo.onclick = nextImage;
}
})();
(function () {
function ImageToggler(id, hiddenInputId) {
this.img = document.getElementById(id);
this.details = document.getElementById(hiddenInputId);
this.images = this.img.getAttribute('data-images');
this.imageindex = this.img.getAttribute('data-image-index');
this._init();
}
ImageToggler.prototype._init = function () {
var that = this;
if (this.images) {
this.images = this.images.split(",");
this.imageindex = (this.imageindex? parseInt(this.imageindex, 10): 0);
this.n = this.images.length;
this._setImg(this.img, this.images[this.imageindex]);
this.img.onclick = function () {that._nextImage.call(that);};
}
}
ImageToggler.prototype._setImg = function(el, src) {
el.src = src;
this.details.value = src;
console.log(src);
}
ImageToggler.prototype._nextImage = function () {
this.imageindex = (this.imageindex + 1 < this.n? this.imageindex + 1: 0);
this._setImg(this.img, this.images[this.imageindex]);
}
var foo = new ImageToggler('foo','details');
var foo2 = new ImageToggler('foo2', 'details2');
})();
<img src="error.jpg" id="foo" data-images="1.jpg,2.jpg,3.jpg" data-image-index="1" />
<input type="hidden" id="details" name="details" />
<img src="error.jpg" id="foo2" data-images="4.jpg,5.jpg,6.jpg" data-image-index="2" />
<input type="hidden" id="details2" name="details2" />
<img src="error.jpg" id="foo" data-images="1.jpg,2.jpg,3.jpg" data-image-index="1" />
<input type="hidden" id="details" name="details" />
<img src="error.jpg" id="foo2" data-images="4.jpg,5.jpg,6.jpg" data-image-index="2" />
<input type="hidden" id="details2" name="details2" /> var foo = new ImageToggler('foo','details'); document.getElementById('foo_2jpg').onclick = function() {
foo._setImg(foo.img, 2);
return false;
}; <a href="2.jpg" id="foo_2jpg">2.jpg</a>
ImageToggler.prototype.setImg = function(idx) {
this.img.src = this.images[idx];
this.details.value = idx;
}
this.setImg(this.images[this.imageindex]);
<a href="2.jpg" id="foo_2jpg">2.jpg</a>
document.getElementById('foo_2jpg').onclick = function() {
foo.setImg.call(foo, 2);
return false;
};
<a href="2.jpg" id="foo_2jpg">2.jpg</a>
<script type="text/javascript">
<!--
(function () {
function ImageToggler(id, hiddenInputId) {
this.img = document.getElementById(id);
this.details = document.getElementById(hiddenInputId);
this.images = this.img.getAttribute('data-images');
this.imageindex = this.img.getAttribute('data-image-index');
this._init();
}
ImageToggler.prototype._init = function () {
var that = this;
if (this.images) {
this.images = this.images.split(",");
this.imageindex = (this.imageindex? parseInt(this.imageindex, 10): 0);
this.n = this.images.length;
this.setImg(this.images[this.imageindex]);
this.img.onclick = function () {that._nextImage.call(that);};
}
}
ImageToggler.prototype.setImg = function(idx) {
this.img.src = this.images[idx];
this.details.value = idx;
}
ImageToggler.prototype._nextImage = function () {
this.imageindex = (this.imageindex + 1 < this.n? this.imageindex + 1: 0);
this.setImg(this.images[this.imageindex]);
}
var foo = new ImageToggler('foo','details');
var foo2 = new ImageToggler('foo2','details2');
})();
document.getElementById('foo_2jpg').onclick = function() {
foo.setImg.call(foo, 2);
return false;
};
</script>
(function () {
function ImageToggler(id, hiddenInputId) {
this.img = document.getElementById(id);
this.details = document.getElementById(hiddenInputId);
this.images = this.img.getAttribute('data-images');
this.imageindex = this.img.getAttribute('data-image-index');
this._init();
}
ImageToggler.prototype._init = function () {
var that = this;
if (this.images) {
this.images = this.images.split(",");
this.imageindex = (this.imageindex? parseInt(this.imageindex, 10): 0);
this.n = this.images.length;
this.setImg(this.imageindex);
this.img.onclick = function () {that._nextImage.call(that);};
}
}
ImageToggler.prototype.setImg = function(idx) {
this.imageindex = idx;
this.img.src = this.images[idx];
this.details.value = idx;
console.log(this.img.src);
}
ImageToggler.prototype._nextImage = function () {
this.imageindex = (this.imageindex + 1 < this.n? this.imageindex + 1: 0);
this.setImg(this.imageindex);
}
var foo = new ImageToggler('foo','details');
var foo2 = new ImageToggler('foo2','details2');
document.getElementById('foo_2jpg').onclick = function() {
foo.setImg.call(foo, 2);
return false;
};
})();