Reply
Help with show/hide (display:block/none) JS code
Old 01-04-2007, 09:45 PM Help with show/hide (display:block/none) JS code
Junior Talker

Posts: 2
Hi everyone,

I have a JS situation I need some help with.

I have the following show/hide JS code working fine (2 iterations):
Code:
function showHidespec1(_myObj,_action) {
    var _myTableObj = document.getElementById((_myObj.parentNode.id).substring(0,(_myObj.parentNode.id).indexOf('_')));
    if (_action == 'show') {
        _myTableObj.style.display = 'block';
        _myObj.parentNode.innerHTML = '<a href="#" onclick="showHidespec1(this,\'hide\'); return false" title="Hide Details"><img src="images/hide_details.gif" width="68" height="9" alt="" border=0></a>';
    }
    if (_action == 'hide') {
        _myTableObj.style.display = 'none';
        _myObj.parentNode.innerHTML = '<a href="#" onclick="showHidespec1(this,\'show\'); return false" title="See Details"><img src="images/see_details.gif" width="68" height="9" alt="" border=0></a>';
    }
}



function showHidespec2(_myObj,_action) {
    var _myTableObj = document.getElementById((_myObj.parentNode.id).substring(0,(_myObj.parentNode.id).indexOf('_')));
    if (_action == 'show') {
        _myTableObj.style.display = 'block';
        _myObj.parentNode.innerHTML = '<a href="#" onclick="showHidespec2(this,\'hide\'); return false" title="Hide Details"><img src="images/hide_details.gif" width="68" height="9" alt="" border=0></a>';
    }
    if (_action == 'hide') {
        _myTableObj.style.display = 'none';
        _myObj.parentNode.innerHTML = '<a href="#" onclick="showHidespec2(this,\'show\'); return false" title="See Details"><img src="images/see_details.gif" width="68" height="9" alt="" border=0></a>';
    }
}
HTML code calling JS:

Code:
<div id="specialsID1_Div" align=left><a href="#" style="cursor:pointer;" onclick="showHidespec1(this,'show'); return false" title="See Details"><img src="images/see_details.gif" width="68" height="9" alt="" border=0></a></div>

<div style="display:none;font-weight:normal;padding-top:10px;padding-bottom:5px;" id="specialsID1">
Text1
</div>




[...]


<div id="specialsID2_Div" align=left><a href="#" style="cursor:pointer;" onclick="showHidespec2(this,'show'); return false" title="See Details"><img src="images/see_details.gif" width="68" height="9" alt="" border=0></a></div>

<div style="display:none;font-weight:normal;padding-top:10px;padding-bottom:5px;" id="specialsID2">
Text2
</div>
I'd like to be able to show and hide both divs (or all if more than 2) via a "View all" link placed anywhere w/in the same page. I've tried a few things but just no luck.

Any JS experts out there have an idea? I'd really appreciate any help offered.

Thanks,
Patrick
patsol33 is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 01-04-2007, 10:45 PM Re: Help with show/hide (display:block/none) JS code
Junior Talker

Posts: 2
I've setup a test URL to visualize it a bit better..
http://www.esolrac.com/test/test.html
patsol33 is offline
Reply With Quote
View Public Profile
 
Old 01-09-2007, 03:36 PM Re: Help with show/hide (display:block/none) JS code
Oneway's Avatar
Skilled Talker

Posts: 73
First, you don't really need 2 functions for this.
Code:
function toggleSpec(objId, cmd) {
  var _myObj = document.getElementById(objId);
  var _myTableObj = document.getElementById((_myObj.parentNode.id).substring(0,(_myObj.parentNode.id).indexOf('_')));

  if (cmd == "hide" || _myTableObj.style.display != 'none') {
    _myTableObj.style.display = 'none';
    _myObj.parentNode.innerHTML = '<a href="#" onclick="toggleSpec(objId,\'\'); return false" title="Hide Details"><img src="images/hide_details.gif" width="68" height="9" alt="" border=0></a>';
  }
  if (cmd == "show" || _myTableObj.style.display == 'none') {
    _myTableObj.style.display = 'block';
    _myObj.parentNode.innerHTML = '<a href="#" onclick="toggleSpec(objId,\'\'); return false" title="See Details"><img src="images/see_details.gif" width="68" height="9" alt="" border=0></a>';
  }
}
The function can now be controlled in two ways. Either with or without the cmd argument filled in. If the cmd argument is empty, the function will look at the elements status and toggle it. If the cmd argument isn't empty then the function will 'override' the elements status and do with it whatever the cmd argument says.
(I hope that makes sense to you ).

Next, you'll have to change the way the function is called. We changed thei arguments of the function, so we'll have to make the proper adjustments to every line of code that calls the function.
Code:
onclick="toggleSpec('specialsID1_Div',''); return false"
And change the first argument to reflect the id of the DIV it is contained in.

To toggle all divs, create an extra function that will receive all of the id's that need showing or hiding via arguments and then for every id call the toggleSpec function.
Be sure to use the cmd argument to override the current elements status.

At the end of this extra function, you can change the calling link, just like you do in the toggleSpec function.

Last edited by Oneway : 01-09-2007 at 03:37 PM.
Oneway is offline
Reply With Quote
View Public Profile
 
Old 01-09-2007, 07:23 PM Re: Help with show/hide (display:block/none) JS code
Average Talker

Posts: 20
Name: Lcpl Weaver.
Location: San Francisco
I've got a code just like that on my site www.m4penguins.com
Go to the 'Characters' page, and view the source. It should be pretty easy to just copy it from there. If you are still having trouble, send me a message, as I have enabled receiving email from other users.

Also... if you like the site, tell your friends about it =)
Reaper Charlie is offline
Reply With Quote
View Public Profile Visit Reaper Charlie's homepage!
 
Reply     « Reply to Help with show/hide (display:block/none) JS code
 

Thread Tools

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

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


Webmaster Resources Marketplace:
Software Development Company | Webhosting.UK.com | Text Link Brokers 


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

 


Page generated in 0.11753 seconds with 12 queries