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

How to change images on click event?

Hello Every one,
i have three small images while clicking on each one the image of background should be changed. I have used javascript Dom along div, for the first two images it works but i don't know how to get right the third one.
Could anyone help?

Expand|Select|Wrap|Line Numbers
  1. <style type="text/css">
  2.     #div1 {
  3.     width:100px;
  4.     height:100px;
  5.     background-image:url(../photo/cake.jpg);
  6.     }
  7. </style>
  8.  
  9. <script language="javascript" type="text/javascript">
  10. function changeDivImage(){
  11.         var imgPath = new String();
  12.         imgPath = document.getElementById("div1").style.backgroundImage;
  13.         if(imgPath == "url(../photo/cake.jpg)" || imgPath == ""){
  14.             document.getElementById("div1").style.backgroundImage = "url(../photo/cake.jpg)";
  15.             }else{    
  16.             document.getElementById("div1").style.backgroundImage = "url(../photo/cake1.jpg)";
  17.  
  18.         }
  19. }
  20.     </script>
  21. </head>
  22.  
  23. <body>
  24.     <div id="div1">
  25.     </div>
  26. <input type="button" value="Change Image" onClick="changeDivImage()" />
Feb 27 '11 #1

✓ answered by dgreenhouse

Oops!

Sorry about that...

Remove the semicolons ";" after each image class definition.
i.e.

Expand|Select|Wrap|Line Numbers
  1.   .image1 {background-image:url(../photo/cake1.jpg)};
  2.   .image2 {background-image:url(../photo/cake2.jpg)};
  3.   .image3 {background-image:url(../photo/cake3.jpg)};
  4.  
  5. // should be...
  6.  
  7.   .image1 {background-image:url(../photo/cake1.jpg);}
  8.   .image2 {background-image:url(../photo/cake2.jpg);}
  9.   .image3 {background-image:url(../photo/cake3.jpg);}
  10.  
  11. // Good practice dictates including the semicolons after each selector as in:
  12. // .image3 {background-image:url(../photo/cake3.jpg);}
  13.  
  14.  

6 4272
dgreenhouse
250 Expert 100+
An easier way would be to change the class name of the div and to have css existent that applies the background.

I think I've got this correct...

i.e.
Expand|Select|Wrap|Line Numbers
  1. <style type="text/css">
  2.     #div1 {
  3.     width:100px;
  4.     height:100px;
  5.     }
  6.   .image1 {background-image:url(../photo/cake1.jpg)};
  7.   .image2 {background-image:url(../photo/cake2.jpg)};
  8.   .image3 {background-image:url(../photo/cake3.jpg)};
  9. </style>
  10.  
  11. <script language="javascript" type="text/javascript">
  12.   function changeDivImage(){
  13.     var elm = document.getElementById("div1");
  14.     var new_class_name = "";
  15.  
  16.     switch (elm.className) {
  17.       case "image1":
  18.         new_class_name = "image2";
  19.         break;
  20.       case "image2":  
  21.         new_class_name = "image3";
  22.         break;
  23.       default:
  24.         new_class_name = "image1";   
  25.     }
  26.  
  27.     elm.className = new_class_name;
  28.   }
  29. </script>
  30. </head>
  31.  
  32. <body>
  33.     <div id="div1" class="image1">
  34.     </div>
  35. <input type="button" value="Change Image" onClick="changeDivImage()" />
  36.  
Feb 28 '11 #2
i can get only the default img the other two does not show up
Feb 28 '11 #3
dgreenhouse
250 Expert 100+
Oops!

Sorry about that...

Remove the semicolons ";" after each image class definition.
i.e.

Expand|Select|Wrap|Line Numbers
  1.   .image1 {background-image:url(../photo/cake1.jpg)};
  2.   .image2 {background-image:url(../photo/cake2.jpg)};
  3.   .image3 {background-image:url(../photo/cake3.jpg)};
  4.  
  5. // should be...
  6.  
  7.   .image1 {background-image:url(../photo/cake1.jpg);}
  8.   .image2 {background-image:url(../photo/cake2.jpg);}
  9.   .image3 {background-image:url(../photo/cake3.jpg);}
  10.  
  11. // Good practice dictates including the semicolons after each selector as in:
  12. // .image3 {background-image:url(../photo/cake3.jpg);}
  13.  
  14.  
Feb 28 '11 #4
that is gr8,but if i click on first img several times then it goes on and on till last img,it should show only one img by one click and then stop.
i have all tags closed still the same problem

thanks for help
Mar 1 '11 #5
dgreenhouse
250 Expert 100+
I'd have to imagine that something is wrong in your code - it works fine for me.
Mar 1 '11 #6
that is a headache, i did copy all code and see nothing wrong.

i will go through once again
Mar 1 '11 #7

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

Similar topics

2
by: Pete | last post by:
I have some funky form/event behavior. Access 97. Split frontend/backend, using Access security. I have the same behavior (or lack of behavior) for the pag_Click() event of two separate pages...
5
by: J McD | last post by:
Hi I have a DataGrid with an ImageButton column. When I click on an imagebutton I get a postback but it doesn't run the OnImgBtnClick method. I can actually comment out the line where I add this...
3
by: Benjamin Gavin | last post by:
Hi all, I recently stumbled upon a bug in the ASP.NET framework handling of ImageButton based postbacks. The issues derives from the fact that IE and Mozilla handle the case of a missing image...
11
by: Sarah | last post by:
I have code that I want to run so that if the result is true, it changes the current button's ".DialogResult" property to "DialogResult.OK". Problem is, when I run this code on the ".Click" event...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
2
by: Kjell Kristiansson | last post by:
I have not been able to figure out how to catch the click event from a button that is added dynamically to a table. In priciple the process is as follows: When I first create the page I analyse...
2
by: Mattbooty | last post by:
Hello, Not sure if anyone else has seen this bug, but I have a form where the entire form is covered with a picturebox. The picturebox has a mouseup event. I also have an open file dialog for...
3
by: Robert W. | last post by:
I'm new to ASP.net programming so excuse my ignorance if the following question seems overly simplistic. I've created a simple Login form with 3 primary WebControls: - A TextBox for the Username...
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...
3
by: GauravGupta | last post by:
i want to know that is it posible to call button click event before page load event on post back.... please help me....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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: 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...

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.