how to apply jQuery events to a content that is dynamically loaded using an Ajax get request











up vote
1
down vote

favorite












so this is the jQuery I want the put in the content I am requesting when the page load:






$(".allComments").hide();

$(".viewAll").click(function(e) {
e.stopImmediatePropagation()

if ($(window).scrollTop() >= 150) {
$([document.documentElement, document.body]).animate({
scrollTop: $(this).offset().top
}, 1000);
}
console.log("view all commment is click")
$(this).parent().siblings('div.allComments').slideToggle();


})





So when the content loads, I want to hide the comment section and the user will be able to press a button to reveal the comments..



the problem is the jQuery is not taking into effects....
I know we can use the jQuery on() event but I do not know to use it, if I want the hide the comments when the page loads..



Any ideas how to achieve that?????










share|improve this question
























  • Let me know if I got it right: you're loading the comments from an AJAX call and you want a button to hide them?
    – J. Almandos
    Nov 11 at 0:30










  • I want the comments to be automatically be hidden when the page is loaded and the button will show the comments when clicked..:)
    – MAAD
    Nov 11 at 6:46















up vote
1
down vote

favorite












so this is the jQuery I want the put in the content I am requesting when the page load:






$(".allComments").hide();

$(".viewAll").click(function(e) {
e.stopImmediatePropagation()

if ($(window).scrollTop() >= 150) {
$([document.documentElement, document.body]).animate({
scrollTop: $(this).offset().top
}, 1000);
}
console.log("view all commment is click")
$(this).parent().siblings('div.allComments').slideToggle();


})





So when the content loads, I want to hide the comment section and the user will be able to press a button to reveal the comments..



the problem is the jQuery is not taking into effects....
I know we can use the jQuery on() event but I do not know to use it, if I want the hide the comments when the page loads..



Any ideas how to achieve that?????










share|improve this question
























  • Let me know if I got it right: you're loading the comments from an AJAX call and you want a button to hide them?
    – J. Almandos
    Nov 11 at 0:30










  • I want the comments to be automatically be hidden when the page is loaded and the button will show the comments when clicked..:)
    – MAAD
    Nov 11 at 6:46













up vote
1
down vote

favorite









up vote
1
down vote

favorite











so this is the jQuery I want the put in the content I am requesting when the page load:






$(".allComments").hide();

$(".viewAll").click(function(e) {
e.stopImmediatePropagation()

if ($(window).scrollTop() >= 150) {
$([document.documentElement, document.body]).animate({
scrollTop: $(this).offset().top
}, 1000);
}
console.log("view all commment is click")
$(this).parent().siblings('div.allComments').slideToggle();


})





So when the content loads, I want to hide the comment section and the user will be able to press a button to reveal the comments..



the problem is the jQuery is not taking into effects....
I know we can use the jQuery on() event but I do not know to use it, if I want the hide the comments when the page loads..



Any ideas how to achieve that?????










share|improve this question















so this is the jQuery I want the put in the content I am requesting when the page load:






$(".allComments").hide();

$(".viewAll").click(function(e) {
e.stopImmediatePropagation()

if ($(window).scrollTop() >= 150) {
$([document.documentElement, document.body]).animate({
scrollTop: $(this).offset().top
}, 1000);
}
console.log("view all commment is click")
$(this).parent().siblings('div.allComments').slideToggle();


})





So when the content loads, I want to hide the comment section and the user will be able to press a button to reveal the comments..



the problem is the jQuery is not taking into effects....
I know we can use the jQuery on() event but I do not know to use it, if I want the hide the comments when the page loads..



Any ideas how to achieve that?????






$(".allComments").hide();

$(".viewAll").click(function(e) {
e.stopImmediatePropagation()

if ($(window).scrollTop() >= 150) {
$([document.documentElement, document.body]).animate({
scrollTop: $(this).offset().top
}, 1000);
}
console.log("view all commment is click")
$(this).parent().siblings('div.allComments').slideToggle();


})





$(".allComments").hide();

$(".viewAll").click(function(e) {
e.stopImmediatePropagation()

if ($(window).scrollTop() >= 150) {
$([document.documentElement, document.body]).animate({
scrollTop: $(this).offset().top
}, 1000);
}
console.log("view all commment is click")
$(this).parent().siblings('div.allComments').slideToggle();


})






javascript jquery html ajax javascript-events






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 0:01









Ele

22.2k42044




22.2k42044










asked Nov 10 at 23:52









MAAD

134




134












  • Let me know if I got it right: you're loading the comments from an AJAX call and you want a button to hide them?
    – J. Almandos
    Nov 11 at 0:30










  • I want the comments to be automatically be hidden when the page is loaded and the button will show the comments when clicked..:)
    – MAAD
    Nov 11 at 6:46


















  • Let me know if I got it right: you're loading the comments from an AJAX call and you want a button to hide them?
    – J. Almandos
    Nov 11 at 0:30










  • I want the comments to be automatically be hidden when the page is loaded and the button will show the comments when clicked..:)
    – MAAD
    Nov 11 at 6:46
















Let me know if I got it right: you're loading the comments from an AJAX call and you want a button to hide them?
– J. Almandos
Nov 11 at 0:30




Let me know if I got it right: you're loading the comments from an AJAX call and you want a button to hide them?
– J. Almandos
Nov 11 at 0:30












I want the comments to be automatically be hidden when the page is loaded and the button will show the comments when clicked..:)
– MAAD
Nov 11 at 6:46




I want the comments to be automatically be hidden when the page is loaded and the button will show the comments when clicked..:)
– MAAD
Nov 11 at 6:46












1 Answer
1






active

oldest

votes

















up vote
0
down vote
















// Load dynamic content
$(document).ready(function(){
var comments = '';
for(var i = 0 ; i < 10; i++){
comments += '<div>('+i+') Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.</div>';
}
$('#comment-section').append(comments).hide();
});

// View all button click
$(document).on('click','#view-all',function(){
$('#comment-section').show();
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<div id="comment-section"></div>
<button id="view-all">View All</button>








share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244574%2fhow-to-apply-jquery-events-to-a-content-that-is-dynamically-loaded-using-an-ajax%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote
















    // Load dynamic content
    $(document).ready(function(){
    var comments = '';
    for(var i = 0 ; i < 10; i++){
    comments += '<div>('+i+') Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.</div>';
    }
    $('#comment-section').append(comments).hide();
    });

    // View all button click
    $(document).on('click','#view-all',function(){
    $('#comment-section').show();
    });

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


    <div id="comment-section"></div>
    <button id="view-all">View All</button>








    share|improve this answer

























      up vote
      0
      down vote
















      // Load dynamic content
      $(document).ready(function(){
      var comments = '';
      for(var i = 0 ; i < 10; i++){
      comments += '<div>('+i+') Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.</div>';
      }
      $('#comment-section').append(comments).hide();
      });

      // View all button click
      $(document).on('click','#view-all',function(){
      $('#comment-section').show();
      });

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


      <div id="comment-section"></div>
      <button id="view-all">View All</button>








      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote












        // Load dynamic content
        $(document).ready(function(){
        var comments = '';
        for(var i = 0 ; i < 10; i++){
        comments += '<div>('+i+') Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.</div>';
        }
        $('#comment-section').append(comments).hide();
        });

        // View all button click
        $(document).on('click','#view-all',function(){
        $('#comment-section').show();
        });

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


        <div id="comment-section"></div>
        <button id="view-all">View All</button>








        share|improve this answer















        // Load dynamic content
        $(document).ready(function(){
        var comments = '';
        for(var i = 0 ; i < 10; i++){
        comments += '<div>('+i+') Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.</div>';
        }
        $('#comment-section').append(comments).hide();
        });

        // View all button click
        $(document).on('click','#view-all',function(){
        $('#comment-section').show();
        });

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


        <div id="comment-section"></div>
        <button id="view-all">View All</button>








        // Load dynamic content
        $(document).ready(function(){
        var comments = '';
        for(var i = 0 ; i < 10; i++){
        comments += '<div>('+i+') Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.</div>';
        }
        $('#comment-section').append(comments).hide();
        });

        // View all button click
        $(document).on('click','#view-all',function(){
        $('#comment-section').show();
        });

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


        <div id="comment-section"></div>
        <button id="view-all">View All</button>





        // Load dynamic content
        $(document).ready(function(){
        var comments = '';
        for(var i = 0 ; i < 10; i++){
        comments += '<div>('+i+') Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.</div>';
        }
        $('#comment-section').append(comments).hide();
        });

        // View all button click
        $(document).on('click','#view-all',function(){
        $('#comment-section').show();
        });

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


        <div id="comment-section"></div>
        <button id="view-all">View All</button>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 1:32









        SilentCoder

        1,84211020




        1,84211020






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244574%2fhow-to-apply-jquery-events-to-a-content-that-is-dynamically-loaded-using-an-ajax%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python