473,385 Members | 2,015 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,385 software developers and data experts.

Two Images changed when link is clicked

tjc0ol
26
Hi guys,
I have here a javascript code that is capable of changing one background image when link is clicked, it goes:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript" language="javascript">
  2. var bgImg = new Array();
  3.  
  4. bgImg[0] = "images/backpink.jpg";
  5. bgImg[1] = "images/backgreen.jpg";
  6. bgImg[2] = "images/backvoilet.jpg";
  7. bgImg[3] = "images/backblue.jpg";
  8. bgImg[4] = "images/backaqua.jpg";
  9. bgImg[5] = "images/backlightgreen.jpg";
  10. bgImg[6] = "";
  11.  
  12. function selectBgImg(whichImage){
  13.     if (document.body){
  14.          document.body.background = bgImg[whichImage];
  15.     }
  16. }
  17. </script>
and my html as follows:
[html]
<body style="background-repeat:repeat-x">
<div id="colored"><!-- Start BgColor Selector -->
<ul>
<li style="margin-right:2em">Choose Background color:</li>
<li><a href="javascript:selectBgImg(0)">Pink</a></li>
<li><a href="javascript:selectBgImg(1)">Green</a></li>
<li><a href="javascript:selectBgImg(2)">Violet</a></li>
<li><a href="javascript:selectBgImg(3)">Blue</a></li>
<li><a href="javascript:selectBgImg(4)">Aqua</a></li>
<li><a href="javascript:selectBgImg(5)">Light Green</a></li>
<li><a href="javascript:selectBgImg(6)">No Color</a></li>
</ul>
</div><!-- end BgColor Selector -->
<div id="main"><!-- start main -->
<div><img src="images/curvepink.gif" alt="" width="778" height="15" /></div>
<div id="frame"><!--frame start -->
[/html]

Now, I need help in adding a javascript that if a certain link will be clicked the image i.e. [html]<div><img src="images/curvepink.gif" alt="" width="778" height="15" /></div>[/html] will be also changed and match according to its background color..

I have curvepink.gif, curvegreen.gif and so on.. stored in my images folder.
Is this possilbe? How will this be done? Thanks in advance. -tj
Aug 8 '08 #1
2 1461
vee10
141 100+
Hi,

U can do this by using two methods

Method 1:
Using Div tag

Expand|Select|Wrap|Line Numbers
  1.  
  2. java script :
  3.  
  4. function selectBgImg(whichImage){
  5. var color;
  6.     if (document.body){
  7.          document.body.background = bgImg[whichImage];
  8. switch(whichImage)
  9. {
  10. case 0:
  11. color = "images/curvepink.jpg"
  12. break;
  13. case 1:
  14. color = "images/curvegreen.jpg"
  15. break;
  16. ..........
  17. }
  18.  document.getElementById("main").innerHTML = "<div><img src='" + 
  19. color + "' alt='' /></div>"
  20.     }
  21.  
  22. html:
  23.  
  24. <div id="main"><!-- start main -->
  25.    <div><img src="Winter.jpg" alt=""  /></div>
  26.  
  27.  
Method 2:
using ID
Specify id for the image
Expand|Select|Wrap|Line Numbers
  1. html:
  2.  
  3. <div id="main"><!-- start main -->
  4.    <div><img src="Winter.jpg" alt="" id="images" /></div>
  5. javascript:
  6. function selectBgImg(whichImage){
  7.     if (document.body){
  8.          document.body.background = bgImg[whichImage];
  9. switch(whichImage)
  10. {
  11. case 0:
  12. color = "images/curvepink.jpg"
  13. break;
  14. case 1:
  15. color = "images/curvegreen.jpg"
  16. break;
  17. ..........
  18. }
  19.          document.getElementById("images").src = color;
  20.              }     
  21.  
  22.  
u can use any of the method to solve ur problem
Aug 8 '08 #2
tjc0ol
26
Hi,

U can do this by using two methods

Method 1:
Using Div tag

Expand|Select|Wrap|Line Numbers
  1.  
  2. java script :
  3.  
  4. function selectBgImg(whichImage){
  5. var color;
  6.     if (document.body){
  7.          document.body.background = bgImg[whichImage];
  8. switch(whichImage)
  9. {
  10. case 0:
  11. color = "images/curvepink.jpg"
  12. break;
  13. case 1:
  14. color = "images/curvegreen.jpg"
  15. break;
  16. ..........
  17. }
  18.  document.getElementById("main").innerHTML = "<div><img src='" + 
  19. color + "' alt='' /></div>"
  20.     }
  21.  
  22. html:
  23.  
  24. <div id="main"><!-- start main -->
  25.    <div><img src="Winter.jpg" alt=""  /></div>
  26.  
  27.  
Method 2:
using ID
Specify id for the image
Expand|Select|Wrap|Line Numbers
  1. html:
  2.  
  3. <div id="main"><!-- start main -->
  4.    <div><img src="Winter.jpg" alt="" id="images" /></div>
  5. javascript:
  6. function selectBgImg(whichImage){
  7.     if (document.body){
  8.          document.body.background = bgImg[whichImage];
  9. switch(whichImage)
  10. {
  11. case 0:
  12. color = "images/curvepink.jpg"
  13. break;
  14. case 1:
  15. color = "images/curvegreen.jpg"
  16. break;
  17. ..........
  18. }
  19.          document.getElementById("images").src = color;
  20.              }     
  21.  
  22.  
u can use any of the method to solve ur problem

Thanks for the reply and for sharing your knowledge to us all. -tj
Aug 9 '08 #3

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

Similar topics

1
by: Derek | last post by:
I wanted to know if anyone had any insight or sample code of creating an AVI file from a directory of JPEG images. I have read the thread that that speaks about creating a MOV file from JPEG images...
7
by: Jon Glazer | last post by:
I know this is sorta reposting but I want to simplify the issue a bit. Take the following script: function replacemain(s){ var x = document.images; var y = document.images; var pictemp =...
2
by: Trent | last post by:
Hey All, I am new to javascript and currently coding a site with scripts that are beyond my level of understanding. The problematic page has thumbnail images that can be clicked on to zoom in....
4
by: moondaddy | last post by:
I have an app where users will upload photos to their shopping cart. When they review their cart I need to include a light weight thumbnail of the image they uploaded. how can I take the image a...
4
by: trinitypete | last post by:
Hi, I have a picture gallery type application that loads up several photographs, due to the file sizes of the images the page load is slow - Yes I know I need to thumbnail and reduce image for...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
3
by: vj | last post by:
how to change images based on action. Even clicking changed images should do respective actions. and while displaying only one image at a time sholud get displayed. I am using three images for a...
4
by: JoJo | last post by:
I followed along in Jeremy Keith's book "DOM Scripting" on how to make a image viewer that changes on clicking links. It is up for demo at: http://jojowebdesign.com/dom1.html My question is...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.