Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery events not working on ajax loaded content

Tags:

The below code works. If there is some better way please let me know. If i use the script content which is present in test.html in the main page where I am loading test.html through ajax. The script doesn't work.

<html> 
    <head>
        <script src='jquerylocation' type='text/javascript'></script>
    </head>
    <body>
        <div id='ajaxload'></div>
        <button class='test'>load content via ajax</button>
    </body>


    <script>
        $(function(){
            $('.test').on('click',function(){
                  $('#ajaxload').load('test.html');
            });            
        });
    </script>
</html>

Test.html:

    <h1 class='heading'>Page via AJAX</h1>

    <script>
        $(function(){
            $('.heading').on('click',function(){
                $(this).css('color','red');  
            });                          
        });
    </script>

we have to load the script along with dynamically loaded content through ajax to work as you require.But disadvantage I felt was each time we send ajax request script loads all the time along with content. But I found only this solution. If anyone knows better solution please reply.

For example if change the code this way it wont work.

<html> 
    <head>
        <script src='jquerylocation' type='text/javascript'></script>
    </head>
    <body>
        <div id='ajaxload'></div>
        <button class='test'>load content via ajax</button>
    </body>


    <script>
        $(function(){
            $('.test').on('click',function(){
                  $('#ajaxload').load('test.html');
            });

            $('.heading').on('click',function(){
                $(this).css('color','red');  
            });                                   
        });
    </script>
</html>

Test.html:

    <h1 class='heading'>Page via AJAX</h1>
like image 685
Susheel Singh Avatar asked Jul 12 '13 17:07

Susheel Singh


People also ask

How can get data from table in Ajax?

Perform a AJAX GET request to get data from serverCreate a MySQL table and insert data. Create HTML form and jQuery script to perform AJAX GET Request to PHP MySQL Server. Write a PHP script to receive request from client and fetch data from MySQL database and send a JSON encoded result to client.

What is ajaxComplete?

The ajaxComplete() method specifies a function to be run when an AJAX request completes. Note: As of jQuery version 1.8, this method should only be attached to document. Unlike ajaxSuccess(), functions specified with the ajaxComplete() method will run when the request is completed, even it is not successful.

How show data from database in PHP using AJAX?

click(function() { debugger; $. ajax({ //data :{action: "showroom"}, url :"index. php", //php page URL where we post this data to view from database type :'POST', success: function(data){ $("#content"). html(data); } }); }); });


1 Answers

It's because you are binding the event on document ready. You have to use a delegate in order for this to work. Like on. It's because .header isn't on the page on the page when it's loaded. So no event is attached.

Your code should look some along the lines of this:

$('body').on('click','.heading',function(){
     $(this).css('color','red');  
});   

It doesn't have to be body, but an element which isn't loaded after document ready, which is a parent of .heading.

like image 134
Peter Rasmussen Avatar answered Sep 21 '22 00:09

Peter Rasmussen