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

Switch IMG Src using JavaScript

3
I am trying to swap the source of the image in the image tag when the onclick event of the A HREF fires off. I know I should have this as a separate function as opposed to trying to do it inline, but that isn't a possibility for what I am doing. If I take out the image swap if, it fires correctly, but with it in, it isn't firing.

Here is the source:

Expand|Select|Wrap|Line Numbers
  1. <table border=1 bordercolor="dddddd" cellspacing=0 cellpadding=1>
  2. <tr bgcolor="AAAAFF">
  3. <td style="width: 25px"><a href="" 
  4.   onclick="
  5.     if (document.getElementById(\'offerHistory\').style.display == \'none\') 
  6.       {document.getElementById(\'offerHistory\').style.display = \'inline\';} 
  7.     else {document.getElementById(\'offerHistory\').style.display = \'none\';};
  8.     if (document.getElementById(\'offerHistory\').style.display == \'none\') 
  9.       { document.getElementById(\'offerHistImage\').SetAttribute("src", "./images/arrow-right-2.png";}
  10.     else{ document.getElementById(\'offerHistImage\').SetAttribute("src", "./images/arrow-right-2.png";};
  11.     return false;">
  12.     <img id="offerHistImage" src="./images/arrow-down-2.png" />
  13.     </a>
  14. </td>
  15. <td>Offer History</td>
  16. </tr>
  17. <tr><td colspan="2">
  18. <div id="offerHistory" style="display: none;">
  19. <table border=1 bordercolor="dddddd" cellspacing=0 cellpadding=1>
  20.  
Any ideas are greatly appreciated. :)
Sep 10 '10 #1
5 2385
Thew
69
Why is it all in a Onclick?

try to put it in a function:

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function fire {
  3.  
  4.  <!-- here your code -->
  5.  
  6. }
  7. </script>
  8. <button onclick="fire()">
Sep 10 '10 #2
sunfun
3
For the purposes of what I am trying to do it isn't possible to put it in a separate function.
Sep 10 '10 #3
JKing
1,206 Expert 1GB
A couple things here.
  • Double quotes inside the string aren't escaped
  • SetAttribute should be setAttribute
  • The brackets for setAttribute were never closed

Expand|Select|Wrap|Line Numbers
  1. <table border="1" bordercolor="dddddd" cellspacing="0" cellpadding="1">
  2. <tr bgcolor="AAAAFF">
  3. <td style="width: 25px"><a href="" 
  4.   onclick="
  5.     if (document.getElementById('offerHistory').style.display == 'none') 
  6. {
  7.     document.getElementById('offerHistory').style.display = 'inline';
  8. else 
  9. {
  10.     document.getElementById('offerHistory').style.display = 'none';
  11. }
  12. if (document.getElementById('offerHistory').style.display == 'none') 
  13.     document.getElementById('offerHistImage').setAttribute('src', './images/arrow-right-2.png');
  14. }
  15. else
  16.     document.getElementById('offerHistImage').setAttribute('src', './images/arrow-right-2.png');
  17. }
  18.     return false;">
  19.     <img id="offerHistImage" src="./images/arrow-down-2.png" />
  20.     </a>
  21. </td>
  22. <td>Offer History</td>
  23. </tr>
  24. <tr><td colspan="2">
  25. <div id="offerHistory" style="display: none;">
  26. <table border=1 bordercolor="dddddd" cellspacing=0 cellpadding=1>
  27.  
Sep 10 '10 #4
sunfun
3
I took the above suggestions and was able to move it to a function as shown below. I also made a few improvements for reusability sake. I still have 2 problems.

1) the image still doesn't swap.
2) the page refreshes. I was under the impression that if I returned false from the function, that it would not refresh the page. Am I mistaken??

TIA

Expand|Select|Wrap|Line Numbers
  1. <SCRIPT LANGUAGE="javascript"> 
  2. <!--
  3. function divHideShow(divName, divImage) {
  4.     if (document.getElementById(divName).style.display == 'none') {
  5.         document.getElementById(divName).style.display = 'inline';
  6.         document.getElementById(divImage).setAttribute('src', 'images/expand.png');
  7.     } else {
  8.         document.getElementById(divName).style.display = 'none';
  9.         document.getElementById(divImage).setAttribute('src', 'images/contract.png');
  10.     }
  11.     return false;
  12. }
  13. //--> 
  14.  
  15. </SCRIPT> 
  16.  
  17.  
  18. <table border="1" bordercolor="dddddd" cellspacing="0" cellpadding="1"> 
  19. <tr bgcolor="AAAAFF"> 
  20. <td style="width: 25px"> 
  21. <a href="" onClick="divHideShow('Report_-_By_Time')"><img id="Report_-_By_Time_Img" src="./images/expand.png" /></a> 
  22. </td> 
  23. <td>Report - By Time</td> 
  24. </tr> 
  25. <tr> 
  26. <td colspan="2"><div id="Report_-_By_Time" style="display: inline;"> 
  27. <table border=1 bordercolor="dddddd" cellspacing=0 cellpadding=1><tr><td>something</td></tr> 
  28.  
Sep 16 '10 #5
JKing
1,206 Expert 1GB
Your function takes two parameters and you only pass it one.
Sep 17 '10 #6

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

Similar topics

2
by: webstar | last post by:
How do you play wave files using java.applet.Applet?
1
by: ochulus | last post by:
Hi! I'm new to Java and I would like to know how to manage files (e.g. rm, mv, mkdir, etc.) using Java for a simple text-based application on Unix. It's just an extension of a tutorial question at...
1
by: Utada P.W. SIU | last post by:
Hi all professional I would like to know how to using java class file inside ASP page I have placed the class file to C:\WINNT\java\trustlib also, I have compiled it to 1.1 version but it...
10
by: asj | last post by:
BIG news from the web services front. Amazon will use web services to tie all its vendors together. The company implementing the system will be using Java/C++ (migrating to all-java later). ...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
8
by: dbaplusplus | last post by:
I worked on web development using java script many many years, so I am now a newbie to javascript. I have a single html page, which is generated dynamically using some programming language. Web...
2
by: asenthil | last post by:
i'm trying to read and write files using java... some errors occurs when i'm trying this code.. Error in java: Cannot find symbol location: class java.io.FileOutputStream FileOutputStream...
2
by: asenthil | last post by:
Hai, i'm a beginner to java... just now i had tried to read and write files using java... and then i had tried to connect a database using jdbc... now i want to export the data's from a...
2
by: SelvaSA | last post by:
Hi All, I am newbie to this forum. I have tried to open excel file using java, but problem is how read contents of excel cells. I have tried same in VB, but i don't know how to do it using...
4
by: BibhuAshish | last post by:
Hi, I wanted to delete a line from xml file which i did it. But after deletion of that line there is a blank space. Again if i am adding another line by using java that blank line remains as...
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: 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
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...
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,...

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.