Solved

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

Posted on 2013-12-09
4
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 43

Accepted Solution

by:
Rob 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 43

Expert Comment

by:Rob
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

735 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