Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7684893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:07:51+00:00 2026-05-31T19:07:51+00:00

Event-delegation must be used to make events work after inserting DOM elements (i.e Ajax

  • 0

Event-delegation must be used to make events work after inserting DOM elements (i.e Ajax response) but what about basic JS instructions?

As you will see, all the instructions are sorted by affected PHP page (to update and improve the code more easily, and not to repeat the same instructions several times).
Here, I want to test if a given class exists. This class doesn’t exist when the JS file is loaded, but will be after inserting the AJAX response.

Code example

// NAVBAR.PHP

if ( $('.className').length) {

$('.button-navbar').show(); }      

 /* FOOTER.PHP
 .... */

// VIEW_NEWS.PHP

$('.ajax_wrapper').on('click', '#view_news tr', function() {

    var id = $(this).attr("id");
    var get_news_request = $.ajax({type: "GET", url: "view_news.php", data: {news_id: id}, dataType: "html"});

    get_news_request.done(function(html) {

        $('.ajax_wrapper').hide('slide', {direction: 'right'}, function(){

            $('.ajax_wrapper').empty();
            $('.ajax_wrapper').hide().html(html);
            $('.ajax_wrapper').show('slide');

        });

    });

    get_news_request.fail(function(jqXHR, textStatus, errorThrown) {

        alert( "Request failed: " + textStatus + " " + errorThrown );
    });

});

Unfortunately,

if ( $('.className').length) {

$('.button-navbar').show(); } 

doesn’t seem to be executed, whereas the class now exists.

Do you have a solution which does not break the JS file architecture ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:07:53+00:00Added an answer on May 31, 2026 at 7:07 pm

    That code isn’t an event handler and doesn’t set up any sort of permanent rule. It’s just straight procedural code, there’s no reason it would get called again automatically. At the time it was executed, there was on such class. Adding a class will not make the code execute again.

    However, you can manually do that check in both places (at the beginning, and in the ajax callback). To avoid duplication, you can put the shared functionality into an actual function:

    function show_navbar_check() {
        if ( $('.className').length)
            $('.button-navbar').show();
    }
    
    show_navbar_check();
    

    And then call it in the ajax callback too:

    $('.ajax_wrapper').on('click', '#view_news tr', function() {
    
        var id = $(this).attr("id");
        var get_news_request = $.ajax({type: "GET", url: "view_news.php", data: {news_id: id}, dataType: "html"});
    
        get_news_request.done(function(html) {
    
            $('.ajax_wrapper').hide('slide', {direction: 'right'}, function(){
    
                $('.ajax_wrapper').empty();
                $('.ajax_wrapper').hide().html(html);
                $('.ajax_wrapper').show('slide');
    
                /* I added the line below */
                show_navbar_check();
            });
    
        });
    
        get_news_request.fail(function(jqXHR, textStatus, errorThrown) {
    
            alert( "Request failed: " + textStatus + " " + errorThrown );
        });
    
    });
    

    Sidenote: there is also a solution to this problem using jQuery.live(), but I didn’t include it because I think the above is better, in part for efficiency reasons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to implement event delegation on blur/focus events, in a similar way to
I'm confused about when you should use event delegation as opposed to out-of-the-box JQuery
I'm having a hard time understanding the DEM (Delegation Event Model) in Java. After
With event delegation in jQuery 1.6, you can respond to events that have bubbled
When delegating events using .on how do I target child elements: I have tried:
Which event fires when DataGrid's source is updating? I've tried DataContextChanged and SourceUpdated but
Is it possible to have event delegation using the HTML5 data attributes in MooTools?
I have a delegation parent that listen for click events in a set of
I'm having some trouble with an Ajax script which disables my jQuery click events.
There are a lot of questions about binding future manipulations to non-existent elements that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.