Solved

jQuery dynmically loading accordion into an ajax div

Posted on 2013-05-30
4
1,086 Views
Last Modified: 2013-05-30
Hi

So i have an jQuery that works fine when it is within a static HTML page but I need to have the accordion loading into a div via ajax how do i  adapt the code bellow to achieve this.

Note in the test i'm using a button to bring in the accordion via a perl script in reality i use another event to submit a query./

HTML
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <meta http-equiv="content-type" content="text/html; charset=windows-1250">
      <meta name="generator" content="PSPad editor, www.pspad.com">
      <title></title>
      	<link rel="stylesheet" href="jquery-ui-1.9.2.custom/development-bundle/themes/base/jquery.ui.all.css">
    	<script src="jquery-ui-1.9.2.custom/development-bundle/jquery-1.8.3.js"></script>
    	<script src="jquery-ui-1.9.2.custom/development-bundle/ui/jquery.ui.core.js"></script>
    	<script src="jquery-ui-1.9.2.custom/development-bundle/ui/jquery.ui.widget.js"></script>
    	<script src="jquery-ui-1.9.2.custom/development-bundle/ui/jquery.ui.accordion.js"></script>
    	<link rel="stylesheet" href="jquery-ui-1.9.2.custom/development-bundle/demos/demos.css">
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    
	<script>                                             
	$(function() {
  $("#button").click(function(){
    alert ("Nng");
    $("#test").load('cgi-bin/ac.pl');
  });
	  $( "#accordion" ).accordion({
	    collapsible: true, 
            active : false, 
	    change: function (e, ui) {
	      $url = $(ui.newHeader[0]).children('a').attr('href');
	      $.get($url, function (data) {
                 console.log(data);
	         $(ui.newHeader[0]).next().html(data);
              });
            }
	  });
	});
	</script> 
                                            
    </head>
    <body>
     <div id="container">
     <input type="button" value = "Get Accodion" id="button">  

    
     <div id="test">
     </div>
    </div>
    
    </body>
    
    </html>
        
                                                

Open in new window


perl script  ac.pl

#!C:/Perl64/bin/perl.exe


print "Content-type:text/html\n\n";

print qq(      <div id="accordion">
            <h3><a id="1" href="index.html" >A html page</a></h2>
                 <div id="d1"></div> 
            <h3><a id="2" href="cgi-bin/env.pl" >A perl script</a></h2>
                 <div id="d2"></div>
      </div>);

Open in new window

0
Comment
Question by:trevor1940
[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 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39208329
Try this :
<script>                                             
	$(function() {
		$("#button").click(function(){
			alert ("Nng1");
			$("#test").load('cgi-bin/ac.pl', function() {
				alert ("Nng2");
				$( "#accordion" ).accordion({
					collapsible: true, 
					active : false, 
					change: function (e, ui) {
						$url = $(ui.newHeader[0]).children('a').attr('href');
						$.get($url, function (data) {
							console.log(data);
							$(ui.newHeader[0]).next().html(data);
						});
					}
				});
			});
		});
	});
</script> 

Open in new window

0
 
LVL 1

Author Comment

by:trevor1940
ID: 39208578
Thanx leakim971 that worked a treat you win the points!

can you tell me what the  difference is between your .load & .ajax like

$.ajax({type:"get",url:"cgi-bin/ac.pl",success: function(data){
 // do accordion
});

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 39208595
http://api.jquery.com/load/
http://api.jquery.com/get/
http://api.jquery.com/post/
http://api.jquery.com/getJSON/

are shorcuts of :

http://api.jquery.com/ajax/

$("#putTheContentHere").load("whichPage.ext", { "ImHereSoItWillUsePOST": "1234" }, mycallback);

Open in new window

same as :
$.ajax({ type:"POST", url:"whichPage.ext", data:{ "ImHereToo": "1234" }, success:mycallback});
var mycallback = function(data) { $("#putTheContentHere").html(data); } 

Open in new window

0
 
LVL 1

Author Closing Comment

by:trevor1940
ID: 39209562
sweet Thanx for the  info
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

Title # Comments Views Activity
multiple selects 23 62
googleapis - jQuery.com and so forth 3 19
Javascript: Setting a dropdown based on another dropdown selection 5 39
modify change color of text 9 35
by Julian Matz As of jQuery (http://jquery.com/) 1.4 the .live() method (http://api.jquery.com/live/) supports custom events as well as some standard JavaScript events that it previously didn't support. Among these is the submit event. Unlike …
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
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…

756 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