Solved

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

Posted on 2013-12-09
4
282 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now