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 8962493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:08:17+00:00 2026-06-15T16:08:17+00:00

Please feel free to edit title or description if I’m not making myself clear.

  • 0

Please feel free to edit title or description if I’m not making myself clear.

Big question today : what exactly does alert('foobar'); on DOM ?!

This is my problem : I have a BIG TREE in database to fetch (3 levels). So in my <form> I load it gradually, with AJAX (you can see it on the picture)HCI
Here is my code (simplified) – I am really sorry to paste a large part of code, but all is needed – :

<script type="text/javascript">
    $(document).ready(function(){
        // generate the CAT box
        $("input[name='or-idgd']").change(function(){
            var idgd = this.value;
            $.post(GLOBAL_HOST+"ajax.php", {'myarg':idgd},
              function(data){
                var form = '';
                var obj = jQuery.parseJSON(data);
                form += '<div>';
                for (var i = 0 ; i < obj.length ; i++) {
                  form += '<label for="'+obj[i].id_cat+'_cat">';
                  form += '<input id="'+obj[i].id_cat+'_cat" type="radio" name="or-idcat"';
                  form += 'value="'+obj[i].id_cat+'" >';
                  form += obj[i].cat_name+'</label>';
                }
                form += '</div>';
                $("div#box_cat").append(form);
              }
            );
        });
          // generate the COMP box
        $("input[name='or-idcat']").change(function(){
          var idcat = this.value;
          $.post(GLOBAL_HOST+"ajax.php", {'myarg2':idcat},
            function(data){
              $("div#box_comp").empty();
              var form = '';
              var obj = jQuery.parseJSON(data);
              form += '<div>';
              for (var i = 0 ; i < obj.length ; i++) {
               form += '<label for="'+obj[i].id_comp+'_comp">';
               form += '<input id="'+obj[i].id_comp+'_comp" type="radio" name="or-idcomp"';
               form += ' value="'+obj[i].id_comp+'">';
               form += obj[i].competence_nom+'</label>';
              }
              form += '</div>';
              $("div#box_comp").append(form);
            }
          );
        }));
<?php   /*** THIS IS THE INTERESTED PART - rebulding the tree cf screenshot ***/
        // When the form is submitted :
        if (!empty($_POST['or-idgd'])) { ?>
            $('#<?php echo $_POST['or-idgd']; ?>_gd').trigger('click');
<?php       if (!empty($_POST['or-idcat'])) { ?>
                //alert('before');
                $('#<?php echo $_POST['or-idcat']; ?>_cat').trigger('click');
                //alert('after');
<?php           if (!empty($_POST['or-idcomp'])) { ?>
                    $('#<?php echo $_POST['or-idcomp']; ?>_comp').prop('checked', true);
<?php           }
            }
        } ?>
    }); // document.ready
</script>

The code above works fine. But, when I submit the form, I rebuild the tree in JQuery, using .trigger() to simulate a click. And here we are : like this, this is not working. Only the cat_box is created (thanks to the gd_box built in PHP), ie only the first .trigger('click') works. Yes, I know, I think I can rebuild it in PHP, but I prefer not to..

But brace yourself : if I uncommented the two alert(), others .trigger() works too, and I can have what I want (cf screenshot).

Two questions :

  1. How the hell alert() allow Jquery to reconsider DOM ? Ok, I get it, thanks.
  2. How can I fix it ? (I tried .live(), .on(), etc …)
  • 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-06-15T16:08:19+00:00Added an answer on June 15, 2026 at 4:08 pm

    An alert halts the browser, which in your case gives the oportunity to:

    1. complete the ajax-request
    2. insert the new elements into the dom*

    You need to add some functionality to make you trigger() wait for the last calls to complete, so it actually can be executed on your element.
    Usually this is done, by handing a callback to the function which is executes once it is completed.

    function doStuff(callback){
       // do Stuff
    
       callback();
    }
    function doMoreStuff(){
       // do more stuff after doStuff() is done
    }
    
    // do stuff and after(!) that do more stuff
    function doStuff(doMoreStuff);
    

    * actually this is put into the eventqueue and is scheduled before the rest of your code. During the alert, JS in the GUI-thread is completely stalled. The evaluation of the ajax-callback is scheduled before the rest of your code (not sure whether you can rely on that).

    EDIT:

    Your code could look like this:

    $("input[name='or-idgd']").change(function(e,callback){
       // first parameter (e) is always the event object
       ...
       // if this eventhandler is called with a callback function given
       // execute it in the end of your function
       if(callback){callback()}
    }
    
    // and your modified trigger:
    
    $('#<?php echo $_POST['or-idgd']; ?>_gd').trigger('change',
          // handing over the second trigger as a callback function
          // which gets executed after the first one is ready
          function(){$('#<?php echo $_POST['or-idcat']; ?>_cat').trigger('click');}
      );
    

    See a minimalistic example of the working callback functionality. The first time, change is triggered with a callback, all subsequent times it’s called normally.

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

Sidebar

Related Questions

OK, I'm not entirely clear how to ask this question, so please feel free
I'm not quite sure how best ask this question, so please feel free to
I had problem even in choosing a title for this question. Please feel free
I'm not sure this is the right forum if it's not please feel free
I hope the title is chosen well enough to ask this question. Feel free
I really have no idea what to title this so someone PLEASE feel free
Sorry for a poor title, feel free to edit. I can't understand what the
I apologize if this question is too generic, if it is please feel free
Please feel free to move this to meta/superuser if this is the wrong place.
[Apologies if my question title does not accurately describe my problem- if you can

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.