Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
99
rated 0 times [  106] [ 7]  / answers: 1 / hits: 16024  / 12 Years ago, fri, may 11, 2012, 12:00:00

How do I dynamically change the body content of an html page when a link is clicked?



So I have 3 html pages that looks like this:



<li><a id=homeContainer href=#>Home</a></li>
<li><a id=testContainer href='#'>test</a></li>
<li><a id=pageContainer href=#>page</a></li>


My div:



<div id='container'>
</div>


The javascript:



$( document ).ready( function() {
$( '#testContainer' ).click( function() {
$( '#container' ).load('test.html');
});


This works fine if i make for all pages a separate function. But how can I make the javascript function taking a page in the load() function instead of hardcoding it?



[Edit]



I dropped my whole page with everything, html, javascript and css here:
jsfiddle


More From » html

 Answers
4

try :



HTML



<li><a id=homeContainer href=home.html>Home</a></li>
<li><a id=testContainer href='test.html'>test</a></li>
<li><a id=pageContainer href=page.html>page</a></li>


Javascript



$(document).ready(function()
{
$( 'li>a[id$=Container]' ).click(function(event)
{
event.preventDefault();
var href = $(this).attr('href');
alert(Loading + href)
$('#container').load(href);
return false;
});
});





Edit



Try out this JSFiddle using JQuery


[#85643] Thursday, May 10, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
domeniccolti

Total Points: 276
Total Questions: 98
Total Answers: 93

Location: India
Member since Fri, May 13, 2022
2 Years ago
domeniccolti questions
Mon, Oct 18, 21, 00:00, 3 Years ago
Thu, Oct 14, 21, 00:00, 3 Years ago
Thu, Jul 15, 21, 00:00, 3 Years ago
Sat, Oct 24, 20, 00:00, 4 Years ago
Thu, Sep 3, 20, 00:00, 4 Years ago
;