20

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>
Susheel Singh
  • 3,687
  • 3
  • 27
  • 62
  • 1
    it's because you're wrapping it in the document.ready function, which won't be called after an AJAX call finishes. you can try adding your event handlers in a success callback function in your load() call, or use deferred events globally defined on your main page's script section – Derek Jul 12 '13 at 17:04
  • please check my question once again I edited it. And please please reply with some code – Susheel Singh Jul 12 '13 at 17:08
  • also make sure you're defining your script tag w/the proper attributes ie ` – Derek Jul 12 '13 at 17:08
  • as per html5 DOCTYPE its fine i guess just – Susheel Singh Jul 12 '13 at 17:09
  • some browsers dont like it though, as HTML5 is not universally implemented (or even agreed upon) yet – Derek Jul 12 '13 at 17:10
  • yeah you are right. But that might not be the problem for this question I guess. – Susheel Singh Jul 12 '13 at 17:12

2 Answers2

58

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.

Peter Rasmussen
  • 14,574
  • 7
  • 43
  • 60
-1

One option is to initialize your script on load success:

$('.test').on('click',function(){
   $('#ajaxload').load('test.html',function(){
      $('body').on('click', '.heading', function(){
          $(this).css('color','red');  
      }); 
    });
 });
Peter Rasmussen
  • 14,574
  • 7
  • 43
  • 60
Elias Escalante
  • 196
  • 2
  • 12