Solved

jQuery dynmically loading accordion into an ajax div

Posted on 2013-05-30
4
978 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
  • 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a pageā€¦
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to dynamically set the form action using jQuery.
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)

707 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

15 Experts available now in Live!

Get 1:1 Help Now