Solved

Select item --> loads new js content into the div

Posted on 2013-12-09
4
284 Views
Last Modified: 2013-12-12
Barebones question - Say I have two divs A and B.
In Div A I have a simple select menu or hyperlinks.

When a link or selector is pressed I want to load into div B new code/content. Too keep it simple it could be loading the following

<script>
$( document ).ready(function() {

$('#B').html("I am displaying sample 1 or 2");
});
</script>

Open in new window




Im my project these are different d3.js graphs.

Essentially I just need to see an example showing how to best load this external code so it runs and displays






A sample of base is here

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>jsLoad</title>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    

<style type="text/css">
html, body {
    width:100%;
    height: 100%, margin:0;
    padding:0;
}
</style>
</head>

<body>
<div id="A" style="width:300px;height:100px;">
    <p>Div A</p>
    <p>Show <a href="#">content 1</a><br>
    Show <a href="#">content 2</a></p>
</div>
<div id="B" style="width:300px;height:500px;">Div B<br></div>


<script>
$( document ).ready(function() {

$('#B').html("I am displaying sample 1");
});
</script>
</body>
</html>

Open in new window

0
Comment
Question by:lvmllc
  • 2
  • 2
4 Comments
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE earned 250 total points
ID: 39707273
You're most of the way there, instead of .html using .load

$( "#B" ).load( "ajax/test.html" );

http://api.jquery.com/load/
0
 
LVL 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 39707292
If it is different for each link then you can utilise the onclick event of each link:

...
<a href="#" onclick="loadData('content1.html');">content 1</a>
<br>
<a href="#" onclick="loadData('content2.html');">content 2</a>
...

$(document).ready(function() {
...
});

function loadData(content) {
    $("#B").load(content);
}

Open in new window

0
 

Author Comment

by:lvmllc
ID: 39712595
I think I have this working but have a question regarding how libraries work.

from my test with jQuery it appears that since I have loaded the lib int he main page I will not have to reload - this is good as I want to avoid duplicate lib loads.

Should this then hold for other js libraries allowing me to load them once on the main page only? In this case that would allow my main page to be the app container for the rest of the project pages.
0
 

Author Comment

by:lvmllc
ID: 39712601
Also is there an easy way to keep it from cache the page it loads?
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question