0

[REVISED]

Hi, thanks for the feedback guys. I revised by question btw. To make things clearer here's my html page (sample only)

<html>
<head>
<body>
     <form>
     <div class="loadAjax"></div>
     <div class="toggle">[form inputs here]</toggle>
     <a href="#" class="btn">Submit</a>
     </form>
     <script>[several js scripts here]</script>
</body>
</html>

And here's my main js file:

$(function() {
     [some lines here]
     $('.toggle').toggle();
     $('.btn').on('click', function(e) {
          e.preventDefault();
          $.ajax({
               url: 'http://example.com/ajaxpage',
               type: 'POST',
               data: $('form').serialize(),
               beforeSend: function() {
                     ...
               },
               success: function(data) {
                     $.getScript('js/main.js');
                     $('.content').html(data);
               }
          });
     });
});

The page that will be loaded in div.loadAjax.

<div class="toggle">ajax page load</div>

Taking this as an example, the toggle on the main page seems to work. But the div.toggle on the div.loadAjax doesn't. I need to set the $.getScipt() in order to reload the main.js file again. Shouldn't the div.loadAjax inherit the main.js since it is loaded already in the first place? Why do I still need to reload it again using the $.getScript()?

NOTE: I'm using ajax to do a form post on a PHP page.

Thanks again!

7
  • $('.content').empty().html(data) ? Commented Dec 23, 2013 at 13:53
  • Im sorry, i dont fully get it. you want to be able to fetch script and just print the Data result from ajax into specific div? i just want to be sure Commented Dec 23, 2013 at 13:53
  • where you have loaded the script?? in the ajax loading url or in the main file to where the data gets loaded?? Commented Dec 23, 2013 at 13:55
  • @user1781041 Wait, what you mean is that all listeners like Click and stuff like that are not working? Commented Dec 23, 2013 at 14:02
  • Show us more of your code. Probably there's just a problem with event handling of dynamical added elements... Commented Dec 23, 2013 at 14:10

2 Answers 2

1

The code for toggle was already executed on your first page load. That will not execute again automatically without a page reload, otherwise you have to trigger it again or should load your script again as you did now.

Try attaching it to click event and trigger it dynamically using .trigger

$(function() {
     [some lines here]

     $(".toggle").on("click", function(){
                           $('.toggle').toggle();
                             });

    $('.toggle').trigger("click");

     $('.btn').on('click', function(e) {
          e.preventDefault();
          $.ajax({
               url: 'http://example.com/ajaxpage',
               type: 'POST',
               data: $('form').serialize(),
               beforeSend: function() {
                     ...
               },
               success: function(data) {

                     $('.content').html(data);
                     $('.toggle').trigger("click");
               }
          });
     });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Why don't you use the $.load() method: http://api.jquery.com/load/

This will let you perform an Ajax call, inject the results back onto the DOM of your page and also supports executing scripts loaded via the request.

1 Comment

Thanks for the feedback. I revised my question though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.