Restart script after load () function

2

I would like to know how to use my script within content that will be loaded by the load() function.

The function:

$(document).ready(function() {
    $('.submenu').click(function(event) {
        event.preventDefault();
        $('.page-wrapper').load($(this).data('href'));
    });
});

The elements (outline):

<section class="page-wrapper">
    <h1> Hello, guys! </h1>
    <p> Este é um exemplo do conteúdo da página </p>

    <ol class="menu">
        <li><a class="submenu" data-href="www.example.com" href="#">Submenu1</a></li>
        <li><a class="submenu" data-href="www.example.com" href="#">Submenu2</a></li>
        <li><a class="submenu" data-href="www.example.com" href="#">Submenu3</a></li>
    </ol>
</section>

That is, the menu will always load via load() . With that my script does not work on the elements that are embedded in my "home page". How do I correct this defect?

    
asked by anonymous 26.08.2014 / 17:12

2 answers

4

Delegate event handling to an ancestor that is permanent on the page:

$(document).ready(function() {
    $('.page-wrapper').on('click', '.submenu', function(event) {
        event.preventDefault();
        $('.page-wrapper').load($(this).data('href'));
    });
});
    
26.08.2014 / 18:34
0

Recently I had the same problem with the project here of the company which is totally dependent on ajax, only way to resolve was to run them again.

  

Note that before binding the click I unhook, avoiding duplicate clicks.

 function scripts() {
        $('.submenu').unbind('click').click(function(event) {
            event.preventDefault();
            $('.page-wrapper').load($(this).data('href'));
        });
    }

    $(document).ajaxComplete(scripts);
    
26.08.2014 / 18:42