473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to make images change using next and previous buttons.

Mary Seelye
This is what I have so far -
I cannot get my next link to work, but my previous link works after you change the pic by clicking on it. I'd like to take the "onclick" out and just use the next and previous links to change the pictures. I have 5 pictures and I can only get two to 'almost' work:

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head>
  5. <title>Townhouse</title>
  6. <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
  7. <link rel="stylesheet" href="pineknoll_styles.css" type="text/css" />
  8. <script type="text/javascript">
  9. /* <![CDATA[ */
  10. var curImage = "townhouse";
  11. var cImages = [
  12. 'townhouse.jpg',
  13. 'townhouse2.jpg',
  14. 'townhouse3.jpg',
  15. 'townhouse4.jpg',
  16. 'townhouse5.jpg'
  17. ];
  18. function townhousePics() {
  19. if (curImage =="townhouse.jpg") {
  20. document.images[0].src = "townhouse2.jpg";
  21. curImage = "townhouse2";
  22. }
  23. else {
  24. document.images[0].src = "townhouse2.jpg";
  25. curImage = "townhouse2";
  26. }
  27. }
  28. /* ]]> */
  29. </script>
  30. </head>
  31. <body>
  32. <table width="350">
  33. <tr>
  34. <td>Townhouse: <strong>$319,000</strong></td>
  35. <td><a href="" onclick="self.close();">Close Window</a></td>
  36.  
  37. </tr>
  38. </table>
  39. <img src="townhouse.jpg" height="233" width="350"
  40. onclick="townhousePics();" alt="photo of a townhouse" /><br />
  41. <p>4 bed, 2 bath, 17,33 square feet, .42 acres</p>
  42. <td><a href="" onclick="button;">Next</a></td>
  43. <td><a href="" onclick="button;">Previous</a></td>
  44.  
  45. </body>
  46. </html>
Oct 16 '10 #1

✓ answered by gits

then you might use a simple logic that we already used in your previous thread here. you could use a counter to make use of the image-sources in the array:

Expand|Select|Wrap|Line Numbers
  1. var curImage = 0;
  2. var cImages = [
  3.     'townhouse.jpg',
  4.     'townhouse2.jpg',
  5.     'townhouse3.jpg',
  6.     'townhouse4.jpg',
  7.     'townhouse5.jpg'
  8. ];
  9.  
  10. function townHousePics(direction) {
  11.     if (direction == 'next') {
  12.         curImage++;
  13.  
  14.         if (curImage == 5) {
  15.             curImage = 0;
  16.         }
  17.     } else {
  18.         curImage--;
  19.         // even reset the counter here when
  20.         // its getting 0
  21.     }
  22.  
  23.     document.images[0].src = cImages[curImage];
  24. }
  25.  
and in your links call the function like:

Expand|Select|Wrap|Line Numbers
  1. <a href="" onclick="townHousePics('next'); return false;">Next</a>
  2. <a href="" onclick="townHousePics('previous'); return false;">Previous</a>

11 19279
gits
5,390 Expert Mod 4TB
so do you just want to use 2 pictures or more?
Oct 17 '10 #2
I'd like to use 5
Oct 17 '10 #3
gits
5,390 Expert Mod 4TB
then you might use a simple logic that we already used in your previous thread here. you could use a counter to make use of the image-sources in the array:

Expand|Select|Wrap|Line Numbers
  1. var curImage = 0;
  2. var cImages = [
  3.     'townhouse.jpg',
  4.     'townhouse2.jpg',
  5.     'townhouse3.jpg',
  6.     'townhouse4.jpg',
  7.     'townhouse5.jpg'
  8. ];
  9.  
  10. function townHousePics(direction) {
  11.     if (direction == 'next') {
  12.         curImage++;
  13.  
  14.         if (curImage == 5) {
  15.             curImage = 0;
  16.         }
  17.     } else {
  18.         curImage--;
  19.         // even reset the counter here when
  20.         // its getting 0
  21.     }
  22.  
  23.     document.images[0].src = cImages[curImage];
  24. }
  25.  
and in your links call the function like:

Expand|Select|Wrap|Line Numbers
  1. <a href="" onclick="townHousePics('next'); return false;">Next</a>
  2. <a href="" onclick="townHousePics('previous'); return false;">Previous</a>
Oct 17 '10 #4
Thank you so much again! You are awesome, I really appreciate all the help!
Oct 17 '10 #5
gits
5,390 Expert Mod 4TB
no problem :) ... that's why we're here for ...

kind regards
Oct 17 '10 #6
Gowth
3
@gits
Hi,
I have to put images in the folder. How can we implement this method?
Apr 12 '19 #7
gits
5,390 Expert Mod 4TB
what folder? you need to explain more what you want to achieve and what you have already - else any answer would just be guesswork.
Apr 12 '19 #8
Gowth
3
@gits
As in the above code you took 5 images in the list right, for my work I have to take images from the systems file folder and use click next and previous links to change images.
Apr 23 '19 #9
gits
5,390 Expert Mod 4TB
well - the list is just the image's names in some folder since we setting the src-attribute of the img-element here. basically the names in the cImages-array are to be seen as paths to the corresponding images. in case you dont want to hardcode that list you would need to create that list programmatically - for example you could send a request where a serverside script would read the folder and returns the paths to fill in the array.

you could even go another way and write a service that passes the current image source and the 'direction' as params to the server and the server just delivers the next or previous image source. then this would be a request per click and then you set the src of the image with the response of your service.
Apr 23 '19 #10
Gowth
3
oh ok I will try. thank you
Apr 23 '19 #11
Sherin
77 64KB
HTML

Expand|Select|Wrap|Line Numbers
  1. <section id="modal">
  2.         <section class="modal-background">
  3.             <div class="close-btn">&times;</div>
  4.             <figure>
  5.                 <img class="img-main" src="public/images/bird.jpg" alt="Bird on a tree">
  6.             </figure>
  7.             <figure class="modal-gallery">
  8.                 <img src="public/images/bird.jpg" alt="Bird on a tree">
  9.                 <img src="public/images/boat.jpg" alt="Boat at night">
  10.                 <img src="public/images/dog.jpg" alt="Dog sitting">
  11.                 <img src="public/images/fox.jpg" alt="Fox sitting">
  12.                 <img src="public/images/island.jpg" alt="Island">
  13.                 <img src="public/images/table-flowers.jpg" alt="Flowers on a table">
  14.                 <img src="public/images/city.jpg" alt="Village during the day">
  15.                 <img src="public/images/prague.jpg" alt="City of Prague at night">
  16.                 <img src="public/images/ducks.jpg" alt="Ducks in a pond">
  17.                 <img src="public/images/squirrel.jpg" alt="Squirrel in the snow">
  18.                 <img src="public/images/lemon.jpg" alt="Lemon slice">
  19.                 <img src="public/images/church.jpg" alt="Small church at night">
  20.             </figure>
  21.             <section class="modal-btns">
  22.                 <button class="modal-btn prev-btn">&lt;</button>
  23.                 <button class="modal-btn next-btn">&gt;</button>
  24.             </section>
  25.         </section>
  26.     </section>
JS

Expand|Select|Wrap|Line Numbers
  1. // TOGGLE IMAGE GALLERY MODAL ON CLICK //
  2.  
  3. var imgOverlays = document.querySelectorAll(".imgs section div");
  4. var mainImg = document.querySelector(".img-main");
  5. var allModalImgs = document.querySelectorAll(".modal-gallery img");
  6. var modal = document.querySelector(".modal-background");
  7. var closeBtn = document.querySelector(".close-btn");
  8. var opacity = 0.4;
  9.  
  10. function resetOpacity() {
  11.     for (i = 0; i < allModalImgs.length; i++) {
  12.         allModalImgs[i].style.opacity = 1;
  13.     }
  14. }
  15.  
  16. document.querySelector("#gallery .imgs").addEventListener("click", function (e) {
  17.     for (i = 0; i < imgOverlays.length; i++) {
  18.         if (e.target == imgOverlays[i]) {
  19.             modal.style.display = "block";
  20.             mainImg.src = e.target.previousElementSibling.src;
  21.         }
  22.     }
  23. });
Apr 17 '20 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Marcel | last post by:
Hello, I'm working on a search application for my website. The website contains a lot of pictures, and a search should return clickable thumbnails. No problems there. My problem started when I...
9
by: Karl Roes | last post by:
Hi All, I would like some advice on Next / Previous record buttons. I have a main form for the client, and a continuous subform listing client transactions. If I open one of these transactions...
6
by: Gary | last post by:
I have an example table with fields and records as shown below: RECORD_ID DATE PRODUCT QUANTITY 1 4/1/05 widget1 50 2 4/2/05 widget1 48 3 ...
1
by: msnews | last post by:
Hi All, My client has following request. I am not sure how to do it. Dynamically from database, we are getting set of images names. Now we want to display them with next and previous buttons....
1
by: David | last post by:
What I envision is this. The user gets a set of records in a data grid Then the user gets to select one of the rows to view the detail information about the record. Then instead of backing...
0
by: Roger23 | last post by:
I am trying to use a wizard for a mulit-process step where i have to navigate to different screens. The user can see that in two modes, read-only and update. when its read-only i want to hide the...
1
by: Reb | last post by:
Hi all, I have not successfully found a Javascript sample for getting next and previous links from a file... I figured that someone must have solved this problem already! ... here is what I'm...
1
by: shapper | last post by:
Hello, Is it possible to use images as Next / Previous buttons on an Asp.Net 2.0 GridView? Thanks, Miguel
8
by: GaryDean | last post by:
I have a Wizard page and need to affect the next and previous buttons from my code-behind. I've googled around and found two solutions, and neither appear to work. I can access the SideBarList...
1
by: mfaisalwarraich | last post by:
hi everybody, im trying to make next and previous buttons. im displaying record based on a search. when any image on the search results is clicked then dat image opened in a new page with details...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.