710
Views
5
Comments
Solved
JQuery doesn't work after Ajax Refresh
Question

I have this code inside a webblock Javascript Property:

$(document).ready(function() {
    
$(".item2-1").click(function() {
    $('html,body').animate({
        scrollTop: $(".index2-2").offset().top - 60},
        'slow');
});

});

 and after I do Ajax Refresh for that item, it doesn't work anymore!


Does anyone have a solution for that?



2019-06-15 21-39-22
Afonso Carvalho
 
MVP
Solution

How about rerunning the Javascript once your Ajax refresh runs? You could encapsulate your logic like this:

function itemClickEvent() {
$(".item2-1").click(function() {
    $('html,body').animate({
        scrollTop: $(".index2-2").offset().top - 60},
        'slow');
});
}
$(document).ready(function() {
    itemClickEvent();
});

And then in your refresh logic you could rerun your event logic to make sure it gets set to the new elements with the RunJavascript action:

2021-03-03 08-52-55
Mohamed Emam

Afonso Carvalho wrote:

How about rerunning the Javascript once your Ajax refresh runs? You could encapsulate your logic like this:

function itemClickEvent() {
$(".item2-1").click(function() {
    $('html,body').animate({
        scrollTop: $(".index2-2").offset().top - 60},
        'slow');
});
}
$(document).ready(function() {
    itemClickEvent();
});

And then in your refresh logic you could rerun your event logic to make sure it gets set to the new elements with the RunJavascript action:

Thanks Afonso, that worked!


2019-06-15 21-39-22
Afonso Carvalho
 
MVP

Hi Mohamed,

The click event gets lost because the refreshed element is "new" to the DOM on the page. Have you tried setting the click event with the "on" method?

Something like this:

$("html").on("click", ".item2-1", function() {
    $('html,body').animate({
        scrollTop: $(".index2-2").offset().top - 60},
        'slow');
});
2021-03-03 08-52-55
Mohamed Emam

Afonso Carvalho wrote:

Hi Mohamed,

The click event gets lost because the refreshed element is "new" to the DOM on the page. Have you tried setting the click event with the "on" method?

Something like this:

$("html").on("click", ".item2-1", function() {
    $('html,body').animate({
        scrollTop: $(".index2-2").offset().top - 60},
        'slow');
});


Yes, I tried it but it's not working as well!

2019-06-15 21-39-22
Afonso Carvalho
 
MVP
Solution

How about rerunning the Javascript once your Ajax refresh runs? You could encapsulate your logic like this:

function itemClickEvent() {
$(".item2-1").click(function() {
    $('html,body').animate({
        scrollTop: $(".index2-2").offset().top - 60},
        'slow');
});
}
$(document).ready(function() {
    itemClickEvent();
});

And then in your refresh logic you could rerun your event logic to make sure it gets set to the new elements with the RunJavascript action:

2021-03-03 08-52-55
Mohamed Emam

Afonso Carvalho wrote:

How about rerunning the Javascript once your Ajax refresh runs? You could encapsulate your logic like this:

function itemClickEvent() {
$(".item2-1").click(function() {
    $('html,body').animate({
        scrollTop: $(".index2-2").offset().top - 60},
        'slow');
});
}
$(document).ready(function() {
    itemClickEvent();
});

And then in your refresh logic you could rerun your event logic to make sure it gets set to the new elements with the RunJavascript action:

Thanks Afonso, that worked!


2019-06-15 21-39-22
Afonso Carvalho
 
MVP

Nice. I could've sworn the on method worked for new elements, but I guess not. Glad you got it sorted out.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.