Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Onclick replace div?

Featured Replies

Hi folks,

 

just wondering if any of the javascript wiz kids have a code for replacing one div of text with another when their corresponding links are clicked?

 

Thanks if you can help! :)

  • Author

Thanks but I want to stick to javascript. There must be a common way?

jQuery is Javascript?

This would be the inefficient and horrible looking way of doing it; search engines won't like it though:

 

<div id="container">This is the homepage<br />...</div>
<a href="#" title="Home" onclick="show('home');return false">Home</a>
<a href="#" title="About" onclick="show('about');return false">About</a>
<a href="#" title="Contact" onclick="show('contact');return false">Contact</a>

 

Then put this in your head and add items/code as necessary:

 

<script type="text/javascript">
function show(page)
{
var html = "";
switch(page)
{
	case "home": html = "This is the homepage<br />...";break;
	case "about": html = "This is the about page<br />...";break;
	case "contact": html = "This is the contact page<br />...";break;
}
document.getElementById('container').innerHTML = html;
}
</script>

  • Author

That's great andyl. It worked a treat! Thanks so much!

 

Do you have any idea how I can make each of the cases 'fade in' so that it has a nice stylish feel?

 

I appreciate this! :)

  • Author

Is there a way I can do it using the code already given to me?

 

Don't want to have to delete everything I've just done!

Do you have any idea how I can make each of the cases 'fade in' so that it has a nice stylish feel?

 

Well, you have to use jQuery or Flash for that - and you said you didn't want jQuery so I'm afraid not!

I'll post a jQuery script tomorrow which will do the same thing - in a nice fadey fashion.

  • Author

I am not that great with Javascript/Jquery so need a more relevant example lee grant. Thanks anyway tho.

 

Looking forward to your suggestion andyl. Your last suggestion was great!

 

Thanks guys! :)

Well, you have to use jQuery or Flash for that - and you said you didn't want jQuery so I'm afraid not!

I'll post a jQuery script tomorrow which will do the same thing - in a nice fadey fashion.

I just want to point out that jQuery, being JavaScript-based, proves that neither jQuery nor Flash is essential for that effect. A fade animation is nothing but a looping setTimeout(). A good example script is on Switch on the Code which shows just how simple it can be

For sheer simplicity's sake, could something like this not just be used?

 

http://www.jsfiddle.net/LSyX4/

 

It would also depend on how many divs you wish to toggle in order to determine the best solution. If it's just two divs, it could be achieved really easily with jquery's toggle function.

  • Author

I have 3 paragraphs of text. One should only be visible at a time. However I will be adding more in the future so it should be simple just to add further paragraphs when needed.

 

I suppose it would make things a lot clearer if I just showed you the page...

 

http://www.aesthetemedia.co.uk/info.html

 

As you can see andyl's suggestion is perfect. I just want it to fade so that it looks more stylish.

 

Appreciate your thoughts guys! :)

Sorry about the delay - had some trouble with an email server which pretty much took out 2 days of my life.

Here's your nice effects version:

 

Put this in the <head> tags:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var speed = 500; // The lower this number, the faster. Time in milliseconds.
var animating = false;

$("a.nav").click(function(){
	if ((!animating) && $("#"+$(this).attr("rel")).is(":hidden"))
	{
		animating = true;
		var requestedDiv = "#"+$(this).attr("rel");
		$("#container").children("div").each(function(){$(this).fadeOut(speed/2)});
		$("#container").animate({height: $(requestedDiv).height()}, speed);
		$(requestedDiv).delay(speed/1.5).fadeIn(speed);
		setTimeout(function(){animating = false}, speed*2);
	}
	return false;
});
});
</script>

<style type="text/css">
#container div{display:none}
#container div#homepage{display:block}
</style>

 

Then this is your HTML:

<div id="container">
<div id="homepage">This is the homepage...<br />Add more content as you wish, you can use any HMTL/CSS </div>
<div id="about">This is the about page...<br />Add more content as you wish...</div>
<div id="contact">
	<form action="" method="post">
	<label>Name</label><input type="text" name="name" /><br />
	<label>Email</label><input type="text" name="email" /><br />
	<label>Message</label><textarea name="message"></textarea><br />
	<p>Contact page...</p>
	</form>
</div>
</div>
<a href="#" class="nav" title="Home" rel="homepage">Home</a>
<a href="#" class="nav" title="About" rel="about">About</a>
<a href="#" class="nav" title="Contact" rel="contact">Contact</a>

 

To add more 'pages'

<div id="newPage">Blah...</div> <!-- Put this inside <div id="container"> -->
<a href="#" class="nav" title="New Page Title" rel="newPage">New Page Link</a> <!-- Put this with your navigation -->

 

Tested on Chrome, Safari and Firefox - briefly.

  • 2 years later...

wow where are the mods?

 

this thread is being flooded with spam posts

 

[sorted - Wickham]

Edited by Wickham

  • 1 month later...

I actually read all of this and came across this little gem:

Generally, you might have thirty days to modify your brain

 

Gotta love google translate!

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.