how to apply jQuery events to a content that is dynamically loaded using an Ajax get request
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?????
javascript jquery html ajax javascript-events
add a comment |
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?????
javascript jquery html ajax javascript-events
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
add a comment |
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?????
javascript jquery html ajax javascript-events
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
javascript jquery html ajax javascript-events
edited Nov 11 at 0:01
Ele
22.7k42044
22.7k42044
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
// 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>
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
// 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>
add a comment |
// 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>
add a comment |
// 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>
// 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>
answered Nov 11 at 1:32
SilentCoder
1,83711020
1,83711020
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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