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?????
javascript jquery html ajax javascript-events
add a comment |
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?????
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 |
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?????
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.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
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
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>
add a comment |
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>
add a comment |
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>
add a comment |
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>
// 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,84211020
1,84211020
add a comment |
add a comment |
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