jQuery not work with Ajax returned code

Kirill Che

I have file with that code

<script language="JavaScript">


function load() { var xmlObj;               
                      if(window.XMLHttpRequest){ 
... ajax function which return result...
 xmlObj.send ('...');}


$(document).ready(function() {
                       $("#desc").click(function() {    
                           $("#contcont").hide();       
                           $("#desccont").fadeIn();
                        });

                       $("#cont").click(function() {    
                           $("#desccont").hide();     
                           $("#contcont").fadeIn();         
                        });
                })


            </script>

And have ajax file show.php/ which returned this code

                              <a id="desc">Desc</a>
                              <a id="cont">Cont</a>


                               <div id="desccont">
                                  desccont
                               </div>

                               <div id="contcont">
                                  contccont
                               </div>

And jQuery not works when i click ids desc and cont. But it works normal when html code into first file

jfriend00

I am guessing that you are loading dynamic content with your ajax call and you are not waiting for the dynamic content to be actually done loading before you try to hook up event handlers on the content. $(document).ready() does not wait for dynamically fetched content to load, only for the original page HTML to load.

Thus, you try to install your event handlers with code like $("#desc").click(...) before #desc is actually in the document. Thus, the code doesn't not find the elements and no click handler is attached.

You can fix this two separate ways:

  1. Don't try to install the event handlers UNTIL the ajax code has actually finished inserting the dynamic content. You would put the click handlers inside the ajax completion function to accomplish this.

  2. Use delegated event handling which will work with content loaded after-the-fact.

The basic concept behind delegated event handling is that you attach the event handler to a static parent object that already exists and is in the document. Ideally you select a parent that is as close as possible to where the dynamic element will be (this is more efficient). Then, the event handler use event "bubbling" to see the events that occur on the child events and, if you use jQuery's .on() properly, you can handle the events that occur on the children. You don't show your the relevant portion of the HTML so we can't recommend the most efficient way to do this.

You can read about how to best use delegated event handling in jQuery in these other answers:

Does jQuery.on() work for elements that are added after the event handler is created?

jquery click event doesn't work

jQuery .live() vs .on() method for adding a click event after loading dynamic html

Should all jquery events be bound to $(document)?

JQuery Event Handlers - What's the "Best" method


Other possible issues:

  1. Your id values are not unique in your document. There must be no more than one element with a given id value.

  2. You have a script error somewhere in your code that prevents the desired script from running. You should be able to see this reported in the debug log.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JQuery dont work on content returned by ajax

From Dev

AJAX/jquery doesn't work on a returned PHP-Site

From Dev

Will a jQuery function will still work on an AJAX call returned HTML table?

From Dev

jquery ajax call returned data

From Dev

undefined returned in alertbox jquery ajax

From Dev

PHP Code Returns Its Script and Doesn't Work in JQuery Ajax

From Dev

the code of ajax dont work

From Dev

jQuery Ajax not work in Safari

From Dev

jQuery ajax returned data: json and html mix?

From Dev

Parsing returned HTML from jQuery AJAX request

From Dev

Unable to parse JSON returned by JQuery ajax call

From Dev

jQuery Ajax Success get returned Array Length?

From Dev

jQuery autocomplete ajax request not displaying returned data

From Dev

Getting the value of a returned string from Ajax JQuery

From Dev

jQuery ajax returned data: json and html mix?

From Dev

jQuery Ajax Success get returned Array Length?

From Dev

jQuery ajax json -- viewing incorrect data returned

From Dev

jquery $.ajax problems with using the returned data

From Dev

When are the different jQuery Ajax error codes returned?

From Dev

get ajax returned value in a jquery function

From Dev

Explaination for ajax/jquery code

From Dev

Code work with Postman but not at localhost Ajax

From Dev

ajax code with echo <script> not work

From Dev

href="#" doesn't work if returned by php ajax function

From Dev

Jquery AJAX post to PHP not work

From Dev

jQuery Ajax Get wont work

From Dev

Mansory not work after jQuery Ajax

From Dev

Accessing JSON returned by php script using jquery ajax

From Dev

AJAX/JQUERY - Split the returned data and place in two seperate divs

Related Related

HotTag

Archive