Change image after click - javascript

I can't create simple gallery photos. After click to small image I need reload image in place for main image.
gallery.js
imgArray = [
'../images/1.jpg',
'../images/2.jpg',
'../images/3.jpg',
'../images/4.jpg',
'../images/5.jpg',
'../images/5.jpg'
];
function changeImage(nameOfImage){
document.getElementById('mainImage').src = imgArray[nameOfImage.id];
}
html
<div align="center" class="mainImageDiv">
<img id="mainImage" src="images/action/1.jpg" alt="1.jpg, 206kB" title="1" height="600" width="500">
</div>
<img src="images/action/min1.jpg" alt="min1.jpg, 18kB" title="min1" height="120" width="100" />
<img src="images/action/min2.jpg" alt="min2.jpg, 18kB" title="min2" height="120" width="100" />

Something like this is the code you need:
http://jsfiddle.net/M2BSu/
var imgArray = [
'http://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Brain_human_sagittal_section.svg/295px-Brain_human_sagittal_section.svg.png',
'http://upload.wikimedia.org/wikipedia/commons/thumb/7/79/Solanum_muricatum_Flower_and_Fruit.jpg/220px-Solanum_muricatum_Flower_and_Fruit.jpg'
];
var counter = 0;
document.getElementById('my_button').onclick = function () {
document.getElementById('mainImage').src = imgArray[counter % imgArray.length];
counter += 1;
}
EDIT It seems I misunderstood the problem. I guess the error is, as people commented, that your id is not a valid array index. You can use a real object instead, like this:
<a href="#" id="a_full_name" onClick="changeImage(this)">
var images = {
'a_full_name': 'path to image',
'another_img_id': 'path to this image'
};
function changeImage(anchor_object){ ... } // what you are passing is not the name of the image
And with that your code should work. Arrays are not very reliable in JavaScript (since they are not real arrays)
EDIT: Once found the problem, I give you this easy solution for your problem. As always, the simpler the better. Don't read any array or whatever, just get the path of the big image, from the path of the small image. You will not have to do any annoying maintenance in parallel of the html and the array. You just have to keep a consistent naming between the thumbnail and the big picture.
html
You don't really need an anchor element. If you want the pointer mouse, change the CSS
<img onClick="changeImage(this)" src="images/action/min1.jpg" title="min1" />
JavaScript
function changeImage(image_clicked){ // you pass the image object
var min_path = image_clicked.src;
// adapt the path to point to the big image, in this case get rid of the "min"
var big_path = min_path.replace("min", "");
document.getElementById('mainImage').src = big_path;
}
Keep it simple.

Related

how to change several images with a single click

Im really new to javascript, and while Im learning, Im trying to change images in a gallery to others with an onclick function.
I have links to different galleries. When I click on link 1, the imgs are one1.jpg, one2.jpg and so on. When I click on link 2, imgs are two1.jpg, two2.jpg, etc.
This is the rough script I made for Link 1:
<script>
function one() {
document.getElementById("foto1").src="one1.jpg";
document.getElementById("foto2").src="one2.jpg";
}
</script>
Link 1
Link 2
<img id="foto1" src="tre1.jpg"></a>
<img id="foto2" src="tre2.jpg"></a>
etc.
I made another function called two() for the second link with same content but changing src to what I want. And for every link I add, I copy the script and change it a little.
I know there is a way to optimize to a single script with variables or something .
Any help?
Thanks
In order to differentiate group of images there must be a pattern.
try to add a class on each img tag to differentiate images for changing their source, and the you can loop through images as follows:
Link 1
Link 2
<img class="img1" id="foto1" src="foto1.jpg">
<img class="img1" id="foto2" src="foto1.jpg">
<img class="img2" id="foto3" src="pic1.jpg">
<img class="img2" id="foto4" src="pic2.jpg">
<script>
function one() {
var img = document.getElementsByClassName("img1");
for (var i in img) {
img[i].src = "foto" + 1 + ".jpg"
}
}
function two() {
var img = document.getElementsByClassName("img2");
for (var i in img) {
img[i].src = "pic" + 1 + ".jpg"
}
}
</script>

How to do a simple image swap with JS?

I'm learning Javascript for my Intro to Programming Concepts class and I decided to make a simple photography portfolio site for my final project. I'm making the gallery now and I'm not sure how swap images.
This is what it looks like (cut off but you get the idea):
screenshot
This is the HTML:
<div id="gallery">
<div>
<img src="images/nature/1.jpg">
<br>
<div class="gallery">
<img src="images/nature/2.jpg">
<img src="images/nature/3.jpg">
<img src="images/nature/4.jpg">
<img src="images/nature/5.jpg">
<img src="images/wedding/1.jpg">
<img src="images/wedding/2.jpg">
<img src="images/wedding/3.jpg">
<img src="images/wedding/5.jpg">
<img src="images/wedding/4.jpg">
</div>
</div>
</div>
How do I make it so when you click an img from .gallery it swaps places with the first img (images/nature/1.jpg)?
The following should take whatever image is currently in the first image and swap it out with the one that is clicked:
var firstImage = document.getElementsByTagName('img')[0],
gallery = document.getElementsByClassName('gallery')[0];
gallery.addEventListener('click', function(e) {
var target = e.target;
if (target.tagName == "IMG") {
var newImage = target.src;
target.src = firstImage.src;
firstImage.src = newImage;
}
});
You can test it out here: https://jsfiddle.net/hxhe90qL/6/
After enough clicking around, however, the images will probably be all out of order. You could change it to only swap the first image by replacing the code inside the if statement to:
firstImage.src = target.src;
I think you mean when you click on swipe div, no matter what pic is showing in .gallery will be replaced by 1.jpg.
If that is the case, you can bind onclick event on .gallery, by getting event object, you can get access to the source dom element that triggers the event.
Then you can use javascript to change src attribute.
var gallery = document.querySelector('.gallery');
gallery.addEventListener('click', function(e) {
var targetImg = e.target;
targetImg.src = 'images/nature/1.jpg';
});

Image gallery thumbnails

I'm new to Java and need to build an image gallery.
I have a catalogue of images: each category has a thumbnail; each category has three images.
Process:
Click on thumbnail, bigPic changes its src to show the image you clicked on.
Three dots below bigPic can be clicked to see the other images in that category (so if another thumbnail is chosen, the srcs they pass onto bigPic will change too)
I have tried a few things and looked around but I cannot seem to make it work.
Here is what I have so far:
<script>
function backColor(a) {
document.getElementById("bigPic").src = a;
}
function varE(e){
var chosen = e;
document.getElementsByName("firstDot").id = chosen;
}
function setM(m) {
var chosenImage = m.id;
document.getElementById("bigPic").src = chosenImage;
}
</script>
bigPic:
<img class="bigPic" id="bigPic">
thumbnail image:
<img class="thumb" src="categoryImageSRC" onclick="backColor('anotherImageSRC'); varE('otherImageSRC')">
dot/circle that shows another image in category:
<img name="firstDot" id="" src="dotImageSRC" onclick="setM(this)">
Any ideas on how to go about this, examples and corrections are very welcome! Thank you!
get element by name returns an array of element so you need to change this line in varE function
document.getElementsByName("firstDot").id = chosen;
to
document.getElementsByName("firstDot")[0].id = chosen;

toggle an image to a html map image by javascript

first of all I wanted to know if it is possible?
I want to change a picture inside a div while mouse is hover on the div to another picture which is a html imagemap.
if it is why this code does not work?
<script>
function chgImg(x) {
x.src = "2.jpg";
x.write('<img src="Untitled" width="320" height="427" border="0" usemap="#map" />');
x.write('<map name="map">');
x.write('<area shape="rect" coords="45,85,104,143" href="http://www.google.com" >');
x.write('<area shape="rect" coords="204,40,299,120" href="http://www.yahoo.com" >');
x.write('<area shape="rect" coords="51,296,121,368" href="http://www.imdb.com" >');
x.write('</map>');
}
function originImg(x) {
x.src = "1.jpg";
}
</script>
I changed quote , less than and greater than by html identities in order to evade nested quotes in the java script function.
thank you for your help.
Assuming x is an img element, the following should suffice :
HTML part:
<img onmouseover="chgImg" onmouseout= "originImg" src="1.jpg">
<map name="map">
<!--You map here-->
</map>
Javascript part
function chgImg(x) {
x.src = "2.jpg";
x.usemap = "map";
}
function originImg(x) {
x.src = "1.jpg";
delete x.usemap;
}
(Nota : not entirely sure about the last statement. Could be x.usemap = "" or x.usemap = null or x.usemap = undefined)
If you really need to dynamically construct a map for your img, I suggest you take a look at document.createElement and Node.appendChild, 2 functions that will allow you to add items to the DOM tree in a clean fashion.
I also recommend you use the Mozilla javascript documentation for the reference regarding these 2 DOM functions and all others, as I find it to be the most comprehensive and clear documentation for standart javascript objects and functions.

Is there a way to determine when 4 images have been loaded using JS?

I have 4 images on a page. I want to trigger a JS event once all 4 images are loaded. I of course can't be sure which order the images will be loaded in, so I can't trigger the event on the last image. One thought was to have a counter, but I can't think of the best way to check when that counter is equal to 4 as I don't like the idea of a setTimeout() checking every 200ms.
Any other ideas?
I'm using jQuery on the site, so I'm thinking that might be some help.
This is the image HTML code:
<img src="/images/hp_image-1.jpg" width="553" height="180" id="featureImg1" />
<img src="/images/hp_image-2.jpg" width="553" height="180" id="featureImg2" />
<img src="/images/hp_image-3.jpg" width="553" height="180" id="featureImg3" />
<img src="/images/hp_image-4.jpg" width="553" height="180" id="featureImg4" />
As regards Salty's example, it's best to use a closure to avoid global variables, like such:
$("img").load(function() {
var count = 0, numImages = $("img").size();
return function () {
if (++count === numImages) {
//All images have loaded
}
};
}());
[Edit]
As per jeroen's comment, I replaced the hardcoded value of 4 (count === 4) with the jQuery size function as to allow for more flexibility within the function.
count=0;
$("img").load(function() {
count++;
if(count==4) { //All images have loaded
//Do something!
}
});

Categories

Resources