TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-02-2007, 04:21 PM   #1 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default Slide menu - show one div open on load.

Using JQuery, I have the following code:
PHP Code:
$(document).ready(function() {
    $(
'div.menu> div').hide(); 
    $(
'div.menu> h3').click(function() {
        $(
this).next('div').slideToggle('fast')
        .
siblings('div:visible').slideUp('fast');
    });
}); 
My HTML:
HTML Code:
<div class="menu">
	<h3>Home</h3>
	<h3>News</h3>
	<h3>Profile</h3>
	<div>Profile list goes here....</div>
	<h3>Services</h3>
	<div>Service list goes here....</div>
	<h3>References</h3>
	<div>References list goes here....</div>
	<h3>Jobs</h3>
	<h3>Contact</h3>
</div>
What code do I have to add, if I want the Services menu for example to be open onload?

Demo of current situation: http://www.phpencoder.org/

Looking forward to suggestions/examples!
Sam Granger is offline  
Reply With Quote
Old 11-02-2007, 04:35 PM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

instead of
PHP Code:
$(this
use:
PHP Code:
$(variablename
And use it in another function that you load on bodyload:
HTML Code:
<body onload="openDiv('services')">
And the function would look pretty basic, and use the variablename when you call the function...
Tanax is offline  
Reply With Quote
Old 11-02-2007, 04:47 PM   #3 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default

thing is, the js doesnt loop - how do I tag the div with the variablename?
Sam Granger is offline  
Reply With Quote
Old 11-02-2007, 05:11 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Add the slideToggle code to onload on <body> perhaps?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 11-02-2007, 06:03 PM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I've no idea what the guys above are trying to do, but to solve your problem just requires a little lateral thinking. The problem is that your current code hides all of the div elements within the menu. Now, we want one of those divs to be open -- what should we do? Easy, open it! Using a nifty selector (jQuery has some really nice ones) we can grab hold of the correct div as easy as pie.

Here's the amended code, I've just added one line (with a comment).
Code:
$(document).ready(function() {
	$('div.menu > div').hide();
	$('div.menu > h3').click(function() {
		$(this).next('div').slideToggle('fast')
		.siblings('div:visible').slideUp('fast');
	});
	
	// Show the Services div by default 
	$('div.menu > h3:contains("Services") + div').show();
});
How's that work for you?

Edit: After reading the jQuery documentation, it's possible to select all of the DIV elements not belonging to the Services heading by modifying your original selector. The modification uses the handy not and contains filters.

PHP Code:
$(document).ready(function() {
    $(
'div.menu > h3:not(:contains("Services")) + div').hide();
    $(
'div.menu > h3').click(function() {
        $(
this).next('div').slideToggle('fast')
        .
siblings('div:visible').slideUp('fast');
    });
}); 

Last edited by Salathe : 11-03-2007 at 02:38 AM.
Salathe is offline  
Reply With Quote
Old 11-05-2007, 04:00 PM   #6 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default

Perfect, thanks Salathe!
Sam Granger is offline  
Reply With Quote
Old 11-06-2007, 02:24 PM   #7 (permalink)
The Acquainted
 
Join Date: Sep 2007
Posts: 126
Thanks: 4
Sam Granger is on a distinguished road
Default

One more question, how can I call the h3 tag that I want opened? Eg. in my HTML file I have the following: menu.js?show=Services for Services?
Sam Granger is offline  
Reply With Quote
Old 11-06-2007, 03:40 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

At first, I thought this was going to be a nightmare but thankfully I was wrong! That said, it's not easy peasy so I'll show you the amended code first and explain what's going on later.

PHP Code:
$(document).ready(function() {

    
// By default, reveal the first menu item automatically.
    // If JavaScript is loaded with menu.js?show=something
    // And the "something" item exists in the menu, then that
    // item will be revealed instead.
    
var szDiv 'div.menu > h3:not(:first) + div';
    var 
aShow = $('head > script[src^="menu.js"]:first').attr('src').match(/show=([^&]*)/);
    if (
aShow && aShow[1] != "" && $('div.menu > h3:contains("' aShow[1] + '")').length 0) {
        
szDiv 'div.menu > h3:not(:contains("' aShow[1] + '")) + div';
    }
    
    $(
szDiv).hide();
    $(
'div.menu > h3').click(function() {
        $(
this).next('div').slideToggle('fast', function() {
            $(
this).siblings('div:visible').slideUp('fast')
        });
    });
    
}); 
Because you're going to be telling the script what to open by changing the HTML, it makes sense to have some kind of default action if/when you don't want to provide an item to show (via ?show=something) or if that item does not exist in your menu. The line starting var szDiv... stores a default selector to use in these cases. I'm not sure if you'll understand the selector syntax but basically by default we'll be telling everything but the first menu item to hide itself.

The next line, starting var aShow... is pretty complicated but shows the power of frameworks like jQuery. I'll go through step by step. $('head > script[src^="menu.js"]:first') is another complicated selector but don't let that put you off. All we're doing here is grabbing the first (if any) element in the HTML document which is a SCRIPT element within the HEAD of your HTML and has the SRC attribute pointing to our menu.js file. The next part, attr('src'), simply grabs the SRC attribute from that SCRIPT element. With match(/show=([^&]*)/) we text that SRC attribute against a regular expression which pulls out the provided show, if there is one.

The next line (the if statement) just checks to see whether we could actually pull out the item to show or not. If we could grab an item to show, then we change szDiv to a new selector which makes jQuery display that item rather than the default.

$(szDiv).hide(); then puts all of the above into action by hiding items which match the szDiv selector. This results in all of the divs being hidden except our chosen one, or the first one (default) if no item is chosen.

The rest of the script is identical to before, simply assigning something to do when the headings are clicked.
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:46 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design