5 Replies - 735 Views - Last Post: 05 April 2012 - 02:58 AM

#1 lil_bugga  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 29-October 10

Dynamic Div Display

Posted 05 April 2012 - 01:57 AM

Hi everyone, wondered if you'd be able to help me. I want to display different div's depending on whats clicked. I've found, followed and adapted my own code from this site Coding a Smooth CSS Expanding Navigation

What I'm aiming for is to have 3 div's that will all display within the same part of the page, but only ever one at a time.

When you first come to the site you see the login_signup div, if you click signup it take you to that page, but if you click on login I want the login_signup div to be hidden and the login_form div be displayed.

Once the user has sucessfully signed into the site, I want to hide both the login_signup and login_form divs and display the logged_in div.

this is my code, there may be a couple of little issues with the content as I've compiled 3 seperate pages into one just to try and make it easier for posting

<html>
<head>	
	<script type="text/Javascript">
        function display (div_name) {
            if (document.getElementByID(login_signup) {
                document.getElementByID(login_form) = "show" &&
                document.getElementById(login_signup) = "hide"
            } elseif (document.getElementById(close_login)){
                document.getElementByID(login_signup) = "show" &&
                document.getElementById(login_form) = "hide"
            }							
    </script>
    <style type="text/css">
#header {
		height:370px;
		width:960px;
		position:absolute;
	}

		/* Nested Within Header Div */
		#logo {
			float:left;
			height:370px;
			left:0px;
			position:absolute;
			top:0px;
			width:845px;
			z-index:1;
		}
		
		#nav {
			float:right;
			position:absolute;
			right:10px;
			text-align:right;			
			top:80px;
			width:220px;
			z-index:2;
		}
		
		#login_signup {
			float:right;
			font-family:"Plasmatica";
			right:50px;
			text-align:right;			
			top:10px;
			width:330px;
			z-index:3;
			background-color:#FF0000;
		}
		
		#login_form {
			float:right;
			font-family:"Plasmatica";
			right:50px;
			text-align:right;			
			top:10px;
			width:330px;
			z-index:3;
		}
		
		#logged_in {
			float:right;
			font-family:"Plasmatica";
			right:50px;
			text-align:right;			
			top:10px;
			width:330px;
			z-index:3;
		}
		/* End Of Divs Nested In Header_Container */
		
		.show {
	display:inline;
}

.hide {
	display:none;
}

    </style>
</head>



<body>    
    <div id="header_container">
	<div id="header">
		<div id="logo">
			<img src="logo.png" />		
		</div><!-- end of logo div -->
		<div id="nav">
			<p class="nav-font">
				<a 	href="index.php">Home</a><br />
				<a 	href="aboutMe.php">About</a><br />
				<a 	href="myPlans.php">My Plans</a><br />
				<a 	href="myProgress.php">My Progress</a><br />
				<a 	href="inventory.php">Inventory</a><br />
				<a 	href="images.php">Images</a><br />
				<a 	href="videos.php">Videos</a>
			</p>
		</div><!-- end of nav div -->
		<div id="login_signup">
				<a href="javascript:display('login_signup')">login</a>
                <a href="signUp.php"> | signup</a>				
        </div>
        <div id="login_form">
        	<input name="username" type="text" value="Username" 
            	onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;" />
            <input name="password" type="password" value="Password" 
            	onfocus="if(!this._haschanged){this.value=''};this._haschanged=true;" />
            <input name="login" type="submit" value="Login" />
            <a href="javascript:display('close_login')"><img src="x.png" id="close_login" /></a>
        </div>
        <div id="logged_in">
        	welcome user | 
            <a href="#"> Sign Out</a>
		</div>
	</div><!-- end of header div -->
</div><!-- end of header_container div -->
</body>
</html>



I think I must have my logic twisted in the display function so if someone can help me to not only find a solution, but explain where I slipped up I'd be grateful, I'm still pretty new to Javasctipt.

Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic Div Display

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2888
  • View blog
  • Posts: 7,533
  • Joined: 08-June 10

Re: Dynamic Div Display

Posted 05 April 2012 - 02:18 AM

View Postlil_bugga, on 05 April 2012 - 10:57 AM, said:

What I'm aiming for is to have 3 div's that will all display within the same part of the page, but only ever one at a time.

When you first come to the site you see the login_signup div, if you click signup it take you to that page, but if you click on login I want the login_signup div to be hidden and the login_form div be displayed.

Once the user has sucessfully signed into the site, I want to hide both the login_signup and login_form divs and display the logged_in div.

question 1: what if Javascript is disabled?

question 2: does it all need to be present on the start page? I would find it more sensible to serve what’s required (either via AJAX or with a regular link). this additionally gets you more structure/clearness on the server side.
Was This Post Helpful? 0
  • +
  • -

#3 lil_bugga  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 29-October 10

Re: Dynamic Div Display

Posted 05 April 2012 - 02:37 AM

I hadn't thought about javascript being disabled, but was going to provide secondary links at the bottom of the page

I don't really follow you on the second question. My intention was for a user to be able to login /logout from any page on the site. To clean the code up I was going to place the javascript, and the header for the site within different php include files. That way I don't have the same information saved for every page.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2888
  • View blog
  • Posts: 7,533
  • Joined: 08-June 10

Re: Dynamic Div Display

Posted 05 April 2012 - 02:41 AM

View Postlil_bugga, on 05 April 2012 - 11:37 AM, said:

My intention was for a user to be able to login /logout from any page on the site.

I’d use a log-in/log-out link for that. this way I don’t even need Javascript. the log-in link would call an AJAX script and if JS were disabled, it would send you to a login page (and retaining the last page’s ID shouldn’t be a problem either).
Was This Post Helpful? 0
  • +
  • -

#5 lil_bugga  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 29-October 10

Re: Dynamic Div Display

Posted 05 April 2012 - 02:45 AM

View PostDormilich, on 05 April 2012 - 09:41 AM, said:

the log-in link would call an AJAX script and if JS were disabled, it would send you to a login page (and retaining the last page’s ID shouldn’t be a problem either).


Could you show me how you'd do that, or post a link to some helpful resources. I've never dealt with AJAX.

Cheers
Ben
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 2888
  • View blog
  • Posts: 7,533
  • Joined: 08-June 10

Re: Dynamic Div Display

Posted 05 April 2012 - 02:58 AM

some arbitrary AJAX tutorial

the slightly tricky part is to code the fallback, but in the end, it’s just as simple. all you need is to prevent the default action (= following the link). you do that by either removing the href attribute or by calling the preventDefault()* method of the event object.



* - resp. IE’s counterpart of that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1