Solved

jQuery dynmically loading accordion into an ajax div

Posted on 2013-05-30
4
1,035 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
html form to write data to csv 24 114
add a 3rd selection to sum 7 29
Can you async load a page from another site in a Marketo Landing Page 4 21
Passport Expiry 3 44
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 …
I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
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)

777 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