Can I sort blogs by the age of their starters?
Swap/restore image while show/hide divs
Hello Guest
  
  • Login
• Register…
• Start blog
  • Who, Where, When
• What can I do?
• What to Read?
  • Polls
• Avatars
• Interests
  • Cities and Countries
• Random blog
• Users search
  • Search
• Games
• Tests
• QAIX
  • Сообщества
• Talxy Chat
• Horoscope
• Online
 
Зарегистрируйся!

QAIX > Javascript web-programming > Swap/restore image while show/hide divs 11 April 2008 18:24:55

  Recent blog posts: 
  They have birthday today: 
  Forums:   
  Discuss: 
  Recent forum topics: 
  Recent forum comments:
  Moderators:

Swap/restore image while show/hide divs

Guest 11 April 2008 18:24:55
 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="JavaScrip­t" type="text/JavaScri­pt">
function openIt(train) {
showIt = document.all[train];
if (showIt.style.displ­ay == "none") {
showIt.style.displa­y = ""
} else {
showIt.style.displa­y = "none"
}
var x=1;
var pics=new Array('images/open.­gif','images/close.g­if');
window.document.ima­ges.this_one.src=pic­s[x];
if (x) { x=0; }
else { x=1; }
}
</script>
</HEAD>
<BODY bgcolor="white">
<a href="#" onclick="Javascript­:openIt('list1'); return false;"><img
src="images/open.gi­f" 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="Javascript­:openIt('list2'); 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="Javascript­:openIt('list3'); 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="Javascript­:openIt('list4'); 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

Add comment
Web.Dev 26 January 2006 06:05:47 permanent link ]
 
bridgemanusa@hotmai­l.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="JavaScrip­t" type="text/JavaScri­pt">

The language attribute is deprecated, just stick with the type
attribute.
function openIt(train) {> showIt = document.all[train];> if (showIt.style.displ­ay == "none") {> showIt.style.displa­y = ""> } else {> showIt.style.displa­y = "none"> }> var x=1;> var pics=new Array('images/open.­gif','images/close.g­if');> window.document.ima­ges.this_one.src=pic­s[x];> if (x) { x=0; }> else { x=1; }> }
[snip]> <a href="#" onclick="Javascript­:openIt('list1'); return false;"><img> src="images/open.gi­f" 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(this, 'list1')">
<div id = "list1" class = "closed">
text
</div>

Javascript:

function toggle(imgElem, divId)
{
if(document.getElem­entById)
{
var divElem = document.getElement­ById(divId);
if(divElem.classNam­e == "closed")
{
imgElem.src = "images/close.gif";­
divElem.className = "opened";
}
else
{
imgElem.src = "images/open.fig";
divElem.className = "closed";
}
}
}

Add comment
Guest 27 January 2006 01:20:55 permanent link ]
 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>Untitled Document</title>
<style type="text/css">
<!--
.closed
{
display: none;
}
.opened
{
display: block;
}
-->
</style>
<script type="text/javascri­pt">
function toggle(imgElem, divId)
{
if(document.getElem­entById)
{
var divElem = document.getElement­ById(divId);
if(divElem.classNam­e == "closed")
{
imgElem.src = "images/close.gif";­
divElem.className = "opened";
}
else
{
imgElem.src = "images/open.gif";
divElem.className = "closed";
}
}
</script>

</head>
<body>
<img src="images/open.gi­f" border="0"
onclick="javascript­:toggle(this,'list1'­)">
<div id="list1" class="closed">text­</div>
</body>
</html>

Add comment
Evertjan. 27 January 2006 01:32:38 permanent link ]
 wrote on 26 jan 2006 in comp.lang.javascrip­t:
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>Untitled Document</title>> <style type="text/css">> <!--

Do not use <!--
.closed> {> display: none;>}> .opened> {> display: block;>}> -->

Do not use --> here
</style>> <script type="text/javascri­pt">> function toggle(imgElem, divId)> {> if(document.getElem­entById)> {> var divElem = document.getElement­ById(divId);> if(divElem.classNam­e == "closed")> {> imgElem.src = "images/close.gif";­> divElem.className = "opened";> }> else> {> imgElem.src = "images/open.gif";>­ divElem.className = "closed";> }> }

You need another } [this is your error !!!!]
</script>>
</head>> <body>> <img src="images/open.gi­f" border="0"

no need for border="0"
onclick="javascript­:toggle(this,'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><title>­Untitled Document</title>
<style type="text/css">
.closed {display:none;}
.opened {display:block;}
</style>
<script type="text/javascri­pt">
function toggle(imgElem, divId) {
if(document.getElem­entById) {
var divElem = document.getElement­ById(divId);
if(divElem.classNam­e == "closed") {
imgElem.src = "images/close.gif";­
divElem.className = "opened";
} else {
imgElem.src = "images/open.gif";
divElem.className = "closed";
}
}
}
</script>

</head><body>
<img src="images/open.gi­f" onclick="toggle(thi­s,'list1')">
<div id="list1" class="closed">text­</div>
</body></html>
===========


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Add comment
Guest 27 January 2006 06:54:23 permanent link ]
 Thanks Evertjan.!

It works perfectly. Sorry for my confusion.

Add comment
Guest 11 April 2008 18:24:55 permanent link ]
 =,B­ lamoooo
hi from russia
Add comment
 

Add new comment

As:
Login:  Password:  
 
 
  
 
Пожалуйста, относитесь к собеседникам уважительно, не используйте нецензурные слова, не злоупотребляйте заглавными буквами, не публикуйте рекламу и объявления о купле/продаже, а также материалы нарушающие сетевой этикет или УК РФ.


QAIX > Javascript web-programming > Swap/restore image while show/hide divs 11 April 2008 18:24:55

see also:
[PHP-DEV] final classes and const…
[PHP-DEV] Registering multi classes in…
[PHP-DEV] PEAR Meeting - Summary online
пройди тесты:
Do you know women?
see also:
Hi! ^^

  Copyright © 2001—2008 QAIX
Idea: Miсhael Monashev
Помощь и задать вопросы можно в сообществе support.qaix.com.
Сообщения об ошибках оставляем в сообществе bugs.qaix.com.
Предложения и комментарии пишем в сообществе suggest.qaix.com.
Информация для родителей.
Write us at:
If you would like to report an abuse of our service, such as a spam message, please .