Html is an event

I am adding code to my page using jQuery AJAX calls. This code is a combination of html and javascript. But I want javascript to be executed only when the html part is ready. But what event occurs when adding html?

Here is an example:

<table id="sampleTable">
   ...
</table>

<script>
  // this code should be executed only when sampleTable is rendered
  $('#sampleTable').hide();
</script>
+3
source share
4 answers

Use the jQuery ready () event:

$(document).ready(function() {
    $('#sampleTable').hide();
}

<edit> It seems impossible to trigger a finished event on any other object than the document, my bad </edit>

This is an option if you are talking about an event raised after a successful Ajax request:

$('#sampleTable').ajaxComplete(function() {
    $(this).hide();
});

Or just copy the style of the displayed table: none; ...

+6
source

javascript "" . , DOM , , .

 $(function() {
     $('#sampleTable').hide();
 });
+2
$(document).ready(function() {
    $('#sampleTable').hide();    
});

http://api.jquery.com/ready/

+1

<table> <script> ? .

HTML <script> html()/load() . script innerHTML ; script -. jQuery , ( ).

It is best to store the static code in a static script so that the caller knows what he should call hide()in the table immediately after completing the AJAX call and inserting the content. If you really need to pass dynamic code to run, keep it separate from HTML, for example. returning a JSON object with HTML and code members.

+1
source

Source: https://habr.com/ru/post/1739047/


All Articles