Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Show/Hide (Toggle) Question
Old 03-20-2007, 09:16 PM Show/Hide (Toggle) Question
Novice Talker

Posts: 8
Trades: 0
Hi All,

I'm trying to get this script to work:

Code:
<script language="javascript" type="text/javascript">
function toggle(item) {
    obj=document.getElementById(item);
    visible=(obj.style.display!="none")
    key=document.getElementById("collapse_" + item);
    if (visible) {
    obj.style.display="none";
    key.innerHTML="[+] Expand";
    }
    else {
    obj.style.display="block";
    key.innerHTML="[-] Collapse";
    }
}
</script>
Here is my current HTML:

Code:
<a id="collapse_collections" href="javascript:toggle('collections')">[+] Expand</a><br />
<div id="collections" style="display:none">
<a href="/site/interests/collections/books">Books</a><br />
<a href="/site/interests/collections/magazines">Magazines</a><br />
<a href="/site/interests/collections/movies">Movies</a><br />
<a href="/site/interests/collections/music">Music</a><br />
<a href="/site/interests/collections/video_games">Video Games</a><br />
<a href="/site/interests/collections/wristwatches">Wristwatches</a><br />
</div>
Right now, when I expand/collapse, the links are showing up as [+] Expand or [-] Collapse. However, I'd like to be able to put any text after "Expand" or "Collapse" and include it as part of the link, so that it could be like so:

[+] Expand Music
[+] Collapse Music
[+] Expand Video Games
[+] Collapse Video Games

etc. The key is probably in this part of the JS:

Code:
key.innerHTML="[+] Expand";
key.innerHTML="[-] Collapse";
However, I'm a JS n00b, and am not sure how to do this... If someone could help me, I'd really appreciate it! Thanks in advance...
Eufonik is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-21-2007, 09:37 AM Re: Show/Hide (Toggle) Question
jito's Avatar
MY LIFE IS 'i' LIFE

Posts: 565
Name: surajit ray
Location: inside the heart of my friends
Trades: 0
HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<script language="javascript" type="text/javascript">
function toggle(item) {
    obj=document.getElementById(item);
    visible=(obj.style.display!="none")
    key=document.getElementById("collapse_" + item);
    if (visible) {
    obj.style.display="none";
    key.innerHTML="[+] Expand_"+item;
    }
    else {
    obj.style.display="block";
    key.innerHTML="[-] Collapse_"+item;
    }
}
</script>
</HEAD>
<BODY>
<a id="collapse_Music" href="javascript:toggle('Music')">[+] Music</a><br />
<div id="Music" style="display:none">
<a href="#">Music1</a><br />
<a href="#">Music2</a><br />
</div>
<a id="collapse_VideoGames" href="javascript:toggle('VideoGames')">[+] Video Games</a><br />
<div id="VideoGames" style="display:none">
<a href="#">Video Games1</a><br />
<a href="#">Video Games2</a><br />
</div>
</BODY>
</HTML>
A bit explanation for you. Here we actually passing the div_id to the javascript function toggle to show and hide the div. we are calling the function using a <a> tag.
Ask if you have any question.

Last edited by jito; 03-21-2007 at 09:44 AM.. Reason: adding explanation
jito is offline
Reply With Quote
View Public Profile
 
Old 03-21-2007, 11:23 AM Re: Show/Hide (Toggle) Question
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,898
Name: Keith Marshall
Location: Connecticut
Trades: 0
Nice solution, this kinda helps me.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 03-21-2007, 11:59 AM Re: Show/Hide (Toggle) Question
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
One thing you will want to do with this type of thing is add an ondblclick event handler. If you click the link twice quickly, you may not always get the desired result without it.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Reply     « Reply to Show/Hide (Toggle) Question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.29131 seconds with 12 queries