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
Declaring several ids in same function
Old 08-03-2008, 05:40 PM Declaring several ids in same function
Novice Talker

Posts: 7
Name: Damien Roche
Trades: 0
Hi.

I am having serious problems trying to get this to work.

I have a javascript drop down menu. When I click on the parent, a div with links is made visible. Now, I have put a div inside the div that pops up yet I can only get one to work.

I am using event handlers and a small js script.

Here's a a very small, rearranged script for clarity:

Code:
<a onclick="showdiv('div1');">Click Me</a>
<div id="div1>
<!--LINKS-->
<div id="div2"></div>
</div>


<script type="text/javascript">

function showdiv(id)
{    
    if(menudiv) menudiv.style.visibility = 'hidden';

    menudiv = document.getElementById(id);
    menudiv.style.visibility = 'visible';
}
</script>
I have tried to declare several 'ids' in the onclick event handler, like ('div1','div2') but that hasn't worked.

Is there a way to declare several ids like shown above or will I have to alter the js script?

Many thanks,

Damien.
DamienRoche is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-03-2008, 06:21 PM Re: Declaring several ids in same function
vangogh's Avatar
Post Impressionist

Posts: 10,689
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
You need to set the variable for menudiv before you check whether it's visible or not.

What's happening is you sent one id into the function and change its visibility to visible. The next time through the function you first check to see if the visibility is hidden, but you're still dealing with the previous id which you've already made visible so the conditional returns false.

Try:

Code:
function showdiv(id)
{    
    menudiv = document.getElementById(id);
    if(menudiv) menudiv.style.visibility = 'hidden';
    menudiv.style.visibility = 'visible';
}
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 08-03-2008, 06:49 PM Re: Declaring several ids in same function
Novice Talker

Posts: 7
Name: Damien Roche
Trades: 0
Sadly, that hasn't sorted it.

Can I add anything to the function, like a loop, that will ensure everything inside this div is made visible.

I have put an <li> in the div that pops up and the <li> is made visible with the div. Yet when I use a <div> in it's place, nothing.

I have also tried this
Code:
onclick="showdiv('div1');showdiv('div2');"
and it only shows the second one.

I would really appreciate it if someone could take a look at the js script:

Code:
var menudiv    = 0;


function showdiv(id)
{    

    if(menudiv) menudiv.style.visibility = 'hidden';

    menudiv = document.getElementById(id);
    
    menudiv.style.visibility = 'visible';
}
Can I extend on the function to make everything inside an element visible and not a single id?

Thanks for your help.
DamienRoche is offline
Reply With Quote
View Public Profile
 
Old 08-03-2008, 06:57 PM Re: Declaring several ids in same function
vangogh's Avatar
Post Impressionist

Posts: 10,689
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
Do you have the inner divs visibility set in the css initially? Hiding the outer div should be enough to hide everything inside. From the way you have the html set up I think you only need set the visibility of the outermost div. Don't add any css for visibility to the inner divs.

Hopefully that works.
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 08-03-2008, 07:03 PM Re: Declaring several ids in same function
Novice Talker

Posts: 7
Name: Damien Roche
Trades: 0
No. I haven't even set the visibility for the inner div and it's already invisible, so at least the parent div has some control over it. Problem is the inner and outer visible. I can only do one at any one time.

...whoa...I sorted it.

I checked the css and it turns out I was using #wrap div{visibility:hidden;} so any divs under the wrap were invisible. I've now specified it like #wrap #div1{} and it's working fine.

Thanks for prompting me to look deeper into the css. Much obliged.
DamienRoche is offline
Reply With Quote
View Public Profile
 
Old 08-03-2008, 10:55 PM Re: Declaring several ids in same function
vangogh's Avatar
Post Impressionist

Posts: 10,689
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
Glad I could help in some way even if it wasn't to give you an actual solution. Mostly glad you got it working.
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Reply     « Reply to Declaring several ids in same function
 

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.70183 seconds with 12 queries