Note that there are some explanatory texts on larger screens.

plurals
  1. POFind a dynamical generated element by listening to a custom event (JavaScript)
    primarykey
    data
    text
    <p>I have a list whose <code>li</code> are generated dynamicaly via AJAX:</p> <pre><code>&lt;ul&gt; &lt;li id="hello"&gt;&lt;/li&gt; &lt;li id="world"&gt;&lt;/li&gt; &lt;li id="helloWorld"&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>I also have a global var in my script like this:</p> <pre><code>var world = 'world'; </code></pre> <p>I want, on document ready, as soon as the list gets loaded, to find the <code>li</code> item with the same id as my global var:</p> <pre><code>$('ul').find('#' + world); </code></pre> <p>I can't use a conventional <code>.on()</code> event listener here since there is no other event to trigger it, except for the end of the loading itself. </p> <p>So my next step was to trigger a custom event in my loading script:</p> <pre><code>$('ul').trigger('loadingDone'); </code></pre> <p>And then listen for it:</p> <pre><code>$('ul').one('loadingDone', theFunctionThatDoesTheSearch ); </code></pre> <p>Problem is, this failed and it completely ignored any of the generated elements.</p> <p>I know that a possible solution would be to run these operations inside the success method of the ajax call, but the current architecture of the app I'm building doesn't allow for that. (I can and do trigger the event inside the success AJAX method, I just cant do the search there)</p> <p>Basically what I am asking for are alternative solutions for allowing the loading script to let me know when it is done and ways to filter trough the generated content without doing the filtering inside the loading script itself.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload