473,761 Members | 7,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Swap/restore image while show/hide divs

Hi All:

I have a very long page of html that I want to take portions and hide
them in divs, then show when a link is clicked. I have the hide show
part working when the link is clicked, however I would really like to
use linked images instead to do the following:

- When open.gif is clicked, the contents of the div show and open.gif
is swapped with close.gif
- subsequently, when close.gif is clicked, the div contents get hidden
again and open.gig replaces close.gif

The tricky part (for me anyways) is to be able to do the image
swap/restore multiple times on the same page. Like I said, the div
show/hide function works fine and I kind of managed to get the imgae to
swap from open to clode but it wont swap back and I figure if I do it
multiple times whe way I wrote it (copied it) there is now way it would
work. Here is some sample code:

<HTML>
<HEAD>
<TITLE>main</TITLE>
<script language="JavaS cript" type="text/JavaScript">
function openIt(train) {
showIt = document.all[train];
if (showIt.style.d isplay == "none") {
showIt.style.di splay = ""
} else {
showIt.style.di splay = "none"
}
var x=1;
var pics=new Array('images/open.gif','imag es/close.gif');
window.document .images.this_on e.src=pics[x];
if (x) { x=0; }
else { x=1; }
}
</script>
</HEAD>
<BODY bgcolor="white" >
<a href="#" onclick="Javasc ript:openIt('li st1'); return false;"><img
src="images/open.gif" name="this_one" border="0"></a><br>
<div id="list1" style="display: none">
This will show/hide list1 when you click the above link
</div>
<a href="#" onclick="Javasc ript:openIt('li st2'); return false;">click
here to toggle</a><br>
<div id="list2" style="display: none">
This will show/hide list2 when you click the above link
</div>
<a href="#" onclick="Javasc ript:openIt('li st3'); return false;">click
here to toggle</a><br>
<div id="list3" style="display: none">
This will show/hide list3 when you click the above link
</div>
<a href="#" onclick="Javasc ript:openIt('li st4'); return false;">click
here to toggle</a>
<div id="list4" style="display: none">
This will show/hide list4 when you click the above link
</div>
</BODY>

If I can make this happen by swapping link text instead of images thats
OK too.

Thanks in advance for your help.

Eric B

Jan 26 '06 #1
4 4230

br**********@ho tmail.com wrote:
Hi All:

- When open.gif is clicked, the contents of the div show and open.gif
is swapped with close.gif
- subsequently, when close.gif is clicked, the div contents get hidden
again and open.gig replaces close.gif

<script language="JavaS cript" type="text/JavaScript">
The language attribute is deprecated, just stick with the type
attribute.
function openIt(train) {
showIt = document.all[train];
if (showIt.style.d isplay == "none") {
showIt.style.di splay = ""
} else {
showIt.style.di splay = "none"
}
var x=1;
var pics=new Array('images/open.gif','imag es/close.gif');
window.document .images.this_on e.src=pics[x];
if (x) { x=0; }
else { x=1; }
} [snip] <a href="#" onclick="Javasc ript:openIt('li st1'); return false;"><img
src="images/open.gif" name="this_one" border="0"></a><br>
<div id="list1" style="display: none">
This will show/hide list1 when you click the above link
</div>


See if you like this better:

CSS:

..closed
{
display: none;
}

..opened
{
display: block;
}

HTML:

<img src = "images/open.gif" onclick = "toggle(thi s, 'list1')">
<div id = "list1" class = "closed">
text
</div>

javascript:

function toggle(imgElem, divId)
{
if(document.get ElementById)
{
var divElem = document.getEle mentById(divId) ;
if(divElem.clas sName == "closed")
{
imgElem.src = "images/close.gif";
divElem.classNa me = "opened";
}
else
{
imgElem.src = "images/open.fig";
divElem.classNa me = "closed";
}
}
}

Jan 26 '06 #2
Thanks, but I think I might still be missing something. The image
doesnt seem to respond to the "onclick" in FireFox and in IE I get an
Object Expected error. Eirher way, nothing seems to happen. Any ideas?

Thanks again, and here is the code.

<html>
<head>
<title>Untitl ed Document</title>
<style type="text/css">
<!--
..closed
{
display: none;
}
..opened
{
display: block;
}
-->
</style>
<script type="text/javascript">
function toggle(imgElem, divId)
{
if(document.get ElementById)
{
var divElem = document.getEle mentById(divId) ;
if(divElem.clas sName == "closed")
{
imgElem.src = "images/close.gif";
divElem.classNa me = "opened";
}
else
{
imgElem.src = "images/open.gif";
divElem.classNa me = "closed";
}
}
</script>

</head>
<body>
<img src="images/open.gif" border="0"
onclick="javasc ript:toggle(thi s,'list1')">
<div id="list1" class="closed"> text</div>
</body>
</html>

Jan 26 '06 #3
wrote on 26 jan 2006 in comp.lang.javas cript:
Thanks, but I think I might still be missing something. The image
doesnt seem to respond to the "onclick" in FireFox and in IE I get an
Object Expected error. Eirher way, nothing seems to happen. Any ideas?

Thanks again, and here is the code.

<html>
<head>
<title>Untitl ed Document</title>
<style type="text/css">
<!--
Do not use <!--
.closed
{
display: none;
}
.opened
{
display: block;
}
-->
Do not use --> here
</style>
<script type="text/javascript">
function toggle(imgElem, divId)
{
if(document.get ElementById)
{
var divElem = document.getEle mentById(divId) ;
if(divElem.clas sName == "closed")
{
imgElem.src = "images/close.gif";
divElem.classNa me = "opened";
}
else
{
imgElem.src = "images/open.gif";
divElem.classNa me = "closed";
}
}
You need another } [this is your error !!!!]
</script>

</head>
<body>
<img src="images/open.gif" border="0"
no need for border="0"
onclick="javasc ript:toggle(thi s,'list1')">
do not use javascript: in an onclick
[unless you also use vbscript on the page]
<div id="list1" class="closed"> text</div> </body>
</html>


This works IE6:

===========
<html><head><ti tle>Untitled Document</title>
<style type="text/css">
.closed {display:none;}
.opened {display:block; }
</style>
<script type="text/javascript">
function toggle(imgElem, divId) {
if(document.get ElementById) {
var divElem = document.getEle mentById(divId) ;
if(divElem.clas sName == "closed") {
imgElem.src = "images/close.gif";
divElem.classNa me = "opened";
} else {
imgElem.src = "images/open.gif";
divElem.classNa me = "closed";
}
}
}
</script>

</head><body>
<img src="images/open.gif" onclick="toggle (this,'list1')" >
<div id="list1" class="closed"> text</div>
</body></html>
===========
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 26 '06 #4
Thanks Evertjan.!

It works perfectly. Sorry for my confusion.

Jan 27 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1889
by: Matthew | last post by:
Hi all I'm looking for a solution that can be used in a calendar/gig guide scenario where each day is represented by a dot image. Now this dot must do the following 1. When the mouse goes over the dot image it swaps to the mouseover dot 2. When the mouse goes out of the dot image it restores back to the original img 3. When the img is clicked the dot image is swapped with another image
4
8009
by: jerryyang_la1 | last post by:
I've found this script that allows be to hide/show form elements.. <script language="JavaScript"><!-- var toggle = true; function show(object) { if (document.layers && document.layers) document.layers.visibility = 'visible'; else if (document.all) { document.all.style.visibility = 'visible';
5
3199
by: dje | last post by:
In the OnClick event on a radioButtonList, I run a javascript to show/hide the appropriate div along with a submit button, which displays as expected. The problem is the submit no longer works on the button. If I quit using the divs, and do a postback on the radiobutton selection, showing/hiding tables instead, the button is ok and continues to work. I've tried a variety of controls instead of the button in a variety of places but it...
1
16757
by: asilverpeach | last post by:
Hey Guys! Found some great scripts here on this topic but have to make to changes to the code that I can't seem to figure out. First, In the following code clicking on the headers shows the div information. I want a nested show hide element though. So when you click on "Do you have carpets to be cleaned?" Small Rooms & Medium Rooms appears (that I got working) But Then when you click on Small rooms or medium rooms i want the three lines...
5
8439
by: ali | last post by:
Hello every one i need you help regarding div hide and show. i am having a link like <a href="#" onClick="expandWin()">show/hide </a> <div id=showHide> </div> within div i have lots of form tags and div elements say n. The problem
2
1978
by: sabbas | last post by:
Sorry for my bad english. this is my second web work and things are a little bit difficult for me, so sorry if you not understand what i want for this two situations. First one is that I have 3 links made by 3 small images. (e.g the links are: videos, music, pics) These links upload 3 different .html pages in the same iframe depends on which link you click. What i want is when you make a click with mouse on the first link e.g.(videos) i want...
5
1785
by: sabbas | last post by:
Sorry for my bad english. this is my second web work and things are a little bit difficult for me, so sorry if you not understand what i want for this two situations. First one is that I have 3 links made by 3 small images. (e.g the links are: videos, music, pics) These links upload 3 different .html pages in the same iframe depends on which link you click. What i want is when you make a click with mouse on the first link e.g.(videos) i want...
2
2148
by: dusk | last post by:
Hi, I have a page with lots of hidden divs which are revealed based on choices made at each 'layer'. So I've used naming convention which represents the order in which each div becomes visible - 'a100', 'a200', 'a300' for the first 'layer'; 'b100', 'b200', 'b300' for the second; 'c100, 'c200', 'c300' for the third ... etc I'm writing the javascript functions to hide and show the appropriate divs and its getting out of hand pretty...
18
4864
by: ryrocks | last post by:
Hi, Im making a 'contact us' page. The user click on the div, this then reveals another larger div displaying more information giving the effect of the box expanding or dropping down. I have 3 starting divs on my page, each one expanding onclick. The show/hide elemant works fine. There is one slight problem... I want the user to be able to click anywhere inside the starting div in order for the hidden div to be revealed. This is ok for the...
0
9345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9775
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8780
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2752
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.