Trying to wrap a function in ".find"
I have a jQuery accordion on my site which is loaded into a div by AJAX after the main page has loaded.
The jQuery library is loaded when the main page is loaded and unless I include the library again just above the code I've shown below, the accordion does not work.
The code below is loaded with the AJAX content and is situated below the accordions' HTML.
I believe that when the library is loaded and the document is ready, a "find" is initiated automatically, but I don't want the extra overhead of loading the library again in the newly retrieved accordion. The question is, how do I make find run after the accordion has been loaded?
Here's the code:
- <script type="text/javascript">
- //<![CDATA[
- $(function() {
- $('ul#accordion li>div:first-child, ul#accordion li div:nth-child(2)').click(function() {
- if ($(this).parent('li').hasClass('current')) {
- //an open drawer was clicked
- $(this).siblings(':nth-child(3)').slideToggle('slow', function() {
- $(this).parent('li').toggleClass('current');
- $(this).parent('li').children('div:nth-child(2)').toggleClass('expand collapse');
- });
- } else {
- // A closed drawer was clicked.
- that = this;
- // Check to see if there are any open drawers.
- if ($('ul#accordion li').hasClass('current')) {
- // Other drawers were open, so do this.
- $('ul#accordion li.current div.content').slideToggle('slow', function() {
- $('ul#accordion li.current').children('div:nth-child(2)').toggleClass('expand collapse');
- $('ul#accordion li.current').toggleClass('current');
- // Open the drawer.
- $(that).siblings(':nth-child(3)').slideToggle('slow', function() {
- $(this).parent('li').toggleClass('current');
- $(this).parent('li').children('div:nth-child(2)').toggleClass('expand collapse');
- });
- });
- } else {
- // Open the drawer.
- $(that).siblings(':nth-child(3)').slideToggle('slow', function() {
- $(this).parent('li').toggleClass('current');
- $(this).parent('li').children('div:nth-child(2)').toggleClass('expand collapse');
- });
- }
- }
- });
- // Now find the Go button
- $('#goContBtn').click(function() {
- // Validate forms.
- // Check whether the following required fields have been completed.
- var err = "", checked, value, that, name, id, desc, skip;
- $('.required').each(function(index) {
- // Get input name for use later
- name = "";
- id = "";
- desc = "";
- that = $(this);
- if($(this).is(":radio")) {
- // check whether any radio of same name is checked
- name = $(this).attr('name');
- $('ul#accordion input:radio[name^='+name+']').each(function(index) {
- //alert("checking radio '"+name+"'");
- if($(this).is(":checked")) {
- checked = 1;
- }else{
- value = $(this).val();
- //alert("radio '"+name+"' was not checked. The value was '"+value+"'.");
- value = "";
- }
- });
- if (checked != 1) {
- // Not checked, so append error message.
- id = $(this).attr('id');
- desc = $('ul#accordion label[for$='+id+']').text();
- err +="<p><b>'"+desc+"'</b> has not been chosen.</p>\n";
- }
- checked = "";
- } else {
- // not radio - must be a text field.
- // check to see if it has content
- if ($(this).val() == "") { // check if field was completed or not
- // It wasn't so append error message.
- id = $(this).attr('id');
- // deal with TIN where only the first text input is linked to the label.
- if ((id.indexOf('f8_') > -1 ) || (id.indexOf('f9_') > -1 ) || (id.indexOf('f10_') > -1 ) || (id.indexOf('f11_') > -1 )) {
- if (err.indexOf('Company TIN') < 0 ) { // if 'Company TIN' is not already flagged in err.
- desc="Company TIN";
- }else{
- skip = 1;
- }
- }else{
- desc = $('ul#accordion label[for='+id+']').text();
- }
- if (skip !== 1) {
- err += "<p><b>"+desc+"</b></p>\n";
- }
- skip = "";
- }
- }
- });
- if (err != "") {
- toggleHelp('hide');
- $('#valMsg').css('visibility', 'visible');
- $('#goContBtn').addClass('failedValidation');
- var valMsg = "<h2>Errors Were Found!</h2>\n\n <b>The following fields need setting:</b>\n<div>"+err+"\n</div>\n";
- $('#valMsg').html(valMsg);
- //alert(err);
- }else{
- alert('Congrats - Passed Validation.');
- $('#goContBtn').addClass('neutral');
- toggleHelp('show');
- }
- });
- });
- $(function() {
- // add tooltip stuff
- $('ul#accordion li div:nth-child(2), ul#accordion li div:first-child').attr('title', 'click to toggle open/close');
- $('ul#accordion li div:first-child').addClass('instruct\-tt');
-
- // initialize accordion
- $('ul#accordion li div.content').slideUp('slow', function($) {});
- $('ul#accordion li div:nth-child(2)').addClass('expand instruct\-tt');
- $('ul#accordion li.default div.content').slideDown('slow', function() {
- $(this).parent().addClass('current');
- $('ul#accordion li.default div:nth-child(2)').toggleClass('collapse expand instruct\-tt');
- });
- // format the go button with default formatting
- $('#goContBtn').addClass('neutral');
- });
- //]]>
- </script>
Many thanks.
Peter
A Linux Admin
Willing to help maintain Linux servers remotely for a tiny consideration into my Paypal account :-)