0

I would like some help displaying contents (to different pages) within one HTML page using JavaScript. This is a sample of what I have found so far: http://www.swan10.nl/stuff/test.htm however instead of displaying "FAQ question #blabla" in the box every time a link is clicked, I would like to display words and images like a normal content. Is there a way to do this?

I tried removing the CreateDiv function and replacing it with HTML codes but it doesn't work.

Thank you in advance :)

0

3 Answers 3

1

Umm, well you would need to use AJAX to pull the data into the page and display it in whatever method you choose. If you want to use a framework look into JQuery. It has nice AJAX functions. Otherwise read HERE

After re-reading your post I think you might just want to choose which div is displayed on a form at one time. This you can achieve by placing all of your divs in the same container. Then toggle their display css property.

Sign up to request clarification or add additional context in comments.

2 Comments

There are some pitfalls in that example, but for what you are looking for I think this may be it.
That's exactly what I'm looking for!! Thanks! I'll try and make it fit my needs. Thank you again :D
1

Using jQuery it's as simple as

$('#divname').load('/path/to/file.html');

Note that the result should probably not include <html> and <head> tags (although you don't seem like you care about well formed HTML code).

I should probably also mention that you shouldn't make the client load content for you, that's what server side code is for.

1 Comment

On the contrary, I am very specific when it comes to coding - clean coding + XHTML (which means well formed HTML code). As said above, it is a test code and it's a code I found from a different website. I tried it but didn't manage to change the display from "FAQ question #blabla" to the content that I want so I just copied the code to show it as a website and posted the question. Nonetheless, thanks for your help.
1

Personally I would use the innerHTML property on one of your elements. It will allow you to add markup to that element. Check it out here: http://www.w3schools.com/jsref/prop_html_innerhtml.asp

<html> 
<head> 
<title>Multiple DIV</title> 
<style type="text/css"> 
DIV#db {  
  border : 1px solid blue; 
  width : 400px; 
  height : 400px; 
} 
</style> 
<script type="text/javascript"> 
  var Content = new Array();
  Content[0] = '<i>test1</i>';
  Content[1] = '<b>test2</b><br><img src =http://www.w3schools.com/images/w3schoolslogo.gif>';
  Content[2] = '<u>test3</u>';
  Content[3] = '<s>test4</s>';

function Toggle(IDS) {  
  document.getElementById('db').innerHTML = Content[IDS];
} 
</script> 
</head> 
<body onLoad="Toggle(0,10)"> 
<a href='#' onClick="Toggle(0)">FAQ #1</a> 
<a href='#' onClick="Toggle(1)">FAQ #2</a> 
<a href='#' onClick="Toggle(2)">FAQ #3</a> 
<a href='#' onClick="Toggle(3)">FAQ #4</a> 
<p /> 
<div id="db"></div>
</body> 
</html> 

I updated it to work all javascripty with the innerHTML

2 Comments

Sounds like a good idea, I'll give this a try! However I still would like to know how to do this using JavaScript. Thanks :)
I used your test site and changed it around to show you how you can use the innerHTML and javascript to make it a little tighter than hiding/showing divs. This will give you a little less overhead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.