There is a function in which when loading the script I go over everything

$("#groups input[type=button]") // где input созданны динамически 

How can I find a dynamically created element using jQuery ?! I have done this before, only then were click or change events.
And I need to bust without any action from the user, immediately at boot.

The fact is that my function should work when the page is loaded and resize:

 $(function () { //... AdaptiveFilter(); // здесь НЕ работает поиск динамических элементов $(window).resize(function() { AdaptiveFilter(); // здесь работает поиск д.э }); //... }); 
And function code:

 function AdaptiveFilter() { if ($(window).width() > 750) { var rowWidth = $("#groups").width(); var countItems = Math.floor(rowWidth / (200 + 5)); var inputOrder = -1; var detailsOrder = 0; $("#groups input[type='button']").each(function() { if (($(this).index()/2) % countItems == 0) { inputOrder += 2; } else { inputOrder++; } $(this).css("order", inputOrder); }); $(".details").each(function() { if((($(this).index()-1)/2) % countItems == 0) { detailsOrder += countItems + 1; } $(this).css("order", detailsOrder); }); $(".details").width(countItems * 200); } else { $(".filterCategory").children().each(function() { $(this).css("order", 0); }); $(".details").width("100%"); } } 

  • one
    "dynamically created element using jQuery" - created before the search? Then no additional action is needed. - Igor
  • @Igor thanks for the hint, but it seems that this is not my point. Added code above - Kobets Matviy

1 answer 1

Items that the first call to AdaptiveFilter(); does not find AdaptiveFilter(); are created later. It would be strange if there were elements that are not yet.

In the question code there is not enough arrows with the words "Dynamic elements are created here!"

 function AdaptiveFilter() { console.log('$("#groups input[type=button]").length = ', $("#groups input[type='button']").length); // что выводится в консоль браузера? ... 
  • In the DOM, they already exist, but do not add styles. Even after full loading of the page. And when resize, everything is fine. - Kobets Matviy 9:01 pm
  • When first loaded, displays = 0, when resize = 13 - Kobets Matviy
  • @KobetsMatviy You, as, will say where they are "dynamically created", or is it a secret? - Igor
  • one
    Actually, call AdaptiveFilter() after creating DOM elements. - Igor
  • one
    Yes. Or pass a handler there, inside of which call AdaptiveFilter() . - Igor