Solved

jQuery dynmically loading accordion into an ajax div

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Angular 2 complaining about map file 7 54
What is assert.deepEqual? 4 39
Don't understand jquery promises 6 36
how to avoid redundand ajax calls 5 0
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…
DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
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…

911 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

24 Experts available now in Live!

Get 1:1 Help Now