473,387 Members | 1,619 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,387 software developers and data experts.

How to make Next Button work and go to another image using JavaScript

I am having trouble making my next button work. On clicking the next button , I need the images to change to the next one. There are three images. I am not sure if I have my javascript code in the wrong place or the location of the images do not work. here is my code below:


Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Lab 5, Part 2</title>
  5. <meta charset = "utf-8" />
  6. <script type = "text/javascript">
  7. var curPic = 0;
  8. var cPic = [
  9.     'http://weblab.kennesaw.edu/seeds/lettuce.gif',
  10.     'http://weblab.kennesaw.edu/seeds/tomato.gif',
  11.     'http://weblab.kennesaw.edu/seeds/carrot.gif'
  12. ];
  13.  
  14. function vegeTables(direction) {
  15.     if (direction == 'next') {
  16.         curPic++;
  17.  
  18.         if (curPic == 3) {
  19.             curPic = 0;
  20.         }
  21.     } else {
  22.         curPic--;
  23.     }
  24.  
  25.     document.images[0].src = cPic[curPic];
  26. }
  27. </script>
  28.  
  29. <style type = "text/css" >
  30.   .vegetable1  {position: absolute; top: 120px; left: 39%;
  31.                z-index: 0;}
  32.   .vegetable2  {position: absolute; top: 120px; left: 39%;
  33.                z-index: 0;}
  34.   .vegetable3  {position: absolute; top: 120px; left: 39%;
  35.                z-index: 0;}
  36.   div.bottom    { height:20px;
  37.     width:100%;
  38.     margin: auto;
  39.     position: absolute;
  40.     top: 550px;
  41.     left: 47%;}
  42.   #veggie1 , #veggie2 , #veggie3 {display: inline-block;}
  43. </style>
  44. </head>
  45. <body>
  46.   <p>
  47.    <img class = "vegetable1"  id = "veggie1"
  48.         src = "/seeds/lettuce.gif"
  49.         alt = "picture of vegetable1" />
  50.    <img class = "vegetable2"  id = "veggie2"
  51.         src = "/seeds/tomato.gif"
  52.         alt = "picture of vegetable2" />
  53.    <img class = "vegetable3"  id = "veggie3"
  54.         src = "/seeds/carrot.gif"
  55.         alt = "picture of vegetable3" />
  56. </p>
  57.  
  58.  <div class = "bottom">
  59.   <input id = "clickme" type = "button" value = "Next"  onclick = "vegeTables('next');" />
  60. </div>
  61.  
  62. </body>
  63. </html>
Mar 18 '16 #1
2 3423
gits
5,390 Expert Mod 4TB
basically it is working - but you should have a look at the 3 img tags in your document's body. remove the 2nd and 3rd and then you see the img that you change. you set the src of that one img only - so the other 2 simply do overlay that.
Mar 24 '16 #2
jmrker
17
Alternative to your code to allow movement forward/backward. (Uncomment for Prev button)
Logic for curPic changed a bit for more pictures (if desired).


Expand|Select|Wrap|Line Numbers
  1.     <!DOCTYPE html>
  2.     <html>
  3.     <head>
  4.     <title>Lab 5, Part 2</title>
  5.     <meta charset = "utf-8" />
  6.  
  7.     <script type = "text/javascript">
  8.     var curPic = 0;
  9.     var cPic = [
  10.         'http://weblab.kennesaw.edu/seeds/lettuce.gif',
  11.         'http://weblab.kennesaw.edu/seeds/tomato.gif',
  12.         'http://weblab.kennesaw.edu/seeds/carrot.gif'
  13.     ];
  14.     function vegeTables(direction) {
  15.         if (direction == 'next') { curPic++; } else { curPic--; }
  16.         if (curPic < 0) { curPic = 2; }
  17.         if (curPic > cPic.length-1) { curPic = 0; }
  18.         document.images[0].src = cPic[curPic];
  19.     }
  20.     </script>
  21.  
  22.     <style type = "text/css" >
  23.       .vegetable1  { position: absolute; top: 120px; left: 39%; z-index: 0;}
  24.       div.bottom    { height:20px; width:100%;  margin: auto; 
  25.                       position: absolute; top: 550px; left: 47%;} 
  26. /*      #veggie1 , #veggie2 , #veggie3 { display: inline-block;} */
  27.     </style>
  28.     </head>
  29.     <body>
  30.      <p>
  31.       <img class = "vegetable1"  id = "veggie1" src = "http://weblab.kennesaw.edu/seeds/lettuce.gif" />
  32.      </p>
  33.      <div class = "bottom">
  34.       <input id = "clickmeN" type = "button" value = "Next"  onclick = "vegeTables('next');" />
  35. <!--      <input id = "clickmeP" type = "button" value = "Prev"  onclick = "vegeTables('prev');" /> -->
  36.      </div>
  37.     </body>
  38.     </html>
Apr 4 '16 #3

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

Similar topics

0
by: Bidarkota | last post by:
Can i access serverside selectbox using javascript in another page. if it is possible please help me with sample code. thanks in advance
3
by: dannyf | last post by:
I have an access database that I need to open scanned jpegs from. I don't care if it opens "in" access or if it just invokes the native application for that file type. I need the button to look at...
1
marshal
by: marshal | last post by:
Hi, I want to save web page as an image by using javascript (or may be in java). i.e. I want to save the image which is generated by the 'Print Screen button' of keyboard for web page. How...
0
by: anuparvathy | last post by:
hi To make a flash button work in asp.net do u need to write any code in the load event of the page?or is it just enough to write the code in the flash action script window? note: i do not...
3
by: hegdepras | last post by:
Hi, I need to load only the image in a JSP without refreshing the entire page. I have an action class which sends the stream of the image and I call this action class from 'src' of <img> tag in...
1
by: Ranjit Kumar | last post by:
Hi guys i want to move image in my website using javascript.How i can do it.
5
by: manishamca | last post by:
i want to make the chociebox uneditable that is the option selected shuld not change after we click a button... plz help me. Thanx in advance
6
by: swethak | last post by:
Hi, I displayed the image taken from database.How to raotate that image using javascript.plz tell that how to start the logic.plz tell that some reference websites.
8
by: mike_solomon | last post by:
I have a button <input type="submit" name="Delete" value="Delete"> This code can not be changed I want to use Javascript to change the type I tried:
3
by: pdeb1234 | last post by:
i am trying to dynamically load the image of a bullet and then manipulate it using a looping function( with setTimeout() and clearTimeout() methods). here's the code>>>> <html> <head> <script...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.