Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't select/click elements created on the fly
    primarykey
    data
    text
    <p>I need to build a <code>DIV</code> and inside write a <code>UL</code> with various <code>LI</code> based on AJAX results from PHP function. So I wrote this code:</p> <pre><code>$(function() { $('#categories-picker').simpleSlideView({duration: 250}); $(".step").click(function() { var id = $(this).attr('data-id'); $.ajax({ type: 'GET', url: Routing.generate('category_subcategories', {parent_id: id}), dataType: "json", success: function(data) { var k = 2; if (data.length != 0) { $("#cstep1").append('&lt;div class="view" id="cstep' + k + '"&gt;&lt;ul id="step' + k + '"&gt;&lt;/ul&gt;&lt;/div&gt;'); var LIs = ""; $.each(data, function(index, value) { $.each(value, function(i, v) { LIs += '&lt;li&gt;&lt;a class="step" data-id="' + i + '" href="#"&gt;' + v + '&lt;/a&gt;&lt;/li&gt;'; }) }); $('#cstep' + k).html(LIs); k++; } } }); }); }); </code></pre> <p>When I call the page for first time this HTML code is built:</p> <pre><code>&lt;div id="categories-picker" class="SimpleSlideView-container"&gt; &lt;div id="cstep1" class="view SimpleSlideView-view SimpleSlideView-view-active"&gt; &lt;h2&gt;Seleccione una categoría&lt;/h2&gt; &lt;ul class="circle" id="step1"&gt; &lt;li&gt;&lt;a data-id="1" class="step" href="#"&gt;Monitors&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="2" class="step" href="#"&gt;Cameras&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="4" class="step" href="#"&gt;Scanners&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="5" class="step" href="#"&gt;Printers&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="6" class="step" href="#"&gt;Mice and Trackballs&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="7" class="step" href="#"&gt;Mac&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="8" class="step" href="#"&gt;PC&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="9" class="step" href="#"&gt;Software&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="10" class="step" href="#"&gt;Components&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="11" class="step" href="#"&gt;Phones &amp;amp;amp; PDAs&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="12" class="step" href="#"&gt;Desktops&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="13" class="step" href="#"&gt;MP3 Players&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="14" class="step" href="#"&gt;Laptops &amp;amp;amp; Notebooks&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="15" class="step" href="#"&gt;Windows&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="16" class="step" href="#"&gt;Macs&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="17" class="step" href="#"&gt;Tablets&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;ul class="view-nav clearfix"&gt; &lt;li class="view-nav-contents pull-left"&gt;&lt;a data-popview="" href="#nav"&gt;Previous&lt;/a&gt;&lt;/li&gt; &lt;li class="view-nav-next pull-right"&gt;&lt;a data-pushview="" href="#how"&gt;Next&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>If I pick for example "Scanners" the AJAX call is made and I get this JSON as result:</p> <pre><code>[{"27":"Test10"},{"28":"Test11"},{"29":"Test12"}] </code></pre> <p>So the same jQuery code should add a <code>DIV</code> element with new values and it does but:</p> <ul> <li>Can't click/select the new elements created and can't find the cause of this behavior</li> <li>The new created <code>DIV</code> is suppose to go after the <code>&lt;div id="cstep1" ...</code> and not inside and it's not</li> </ul> <p>This is the code generated after make the first AJAX call:</p> <pre><code>&lt;div id="cstep1" class="view SimpleSlideView-view SimpleSlideView-view-active"&gt; &lt;h2&gt;Seleccione una categoría&lt;/h2&gt; &lt;ul class="circle" id="step1"&gt; &lt;li&gt;&lt;a data-id="1" class="step" href="#"&gt;Monitors&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="2" class="step" href="#"&gt;Cameras&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="4" class="step" href="#"&gt;Scanners&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="5" class="step" href="#"&gt;Printers&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="6" class="step" href="#"&gt;Mice and Trackballs&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="7" class="step" href="#"&gt;Mac&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="8" class="step" href="#"&gt;PC&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="9" class="step" href="#"&gt;Software&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="10" class="step" href="#"&gt;Components&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="11" class="step" href="#"&gt;Phones &amp;amp;amp; PDAs&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="12" class="step" href="#"&gt;Desktops&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="13" class="step" href="#"&gt;MP3 Players&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="14" class="step" href="#"&gt;Laptops &amp;amp;amp; Notebooks&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="15" class="step" href="#"&gt;Windows&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="16" class="step" href="#"&gt;Macs&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a data-id="17" class="step" href="#"&gt;Tablets&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;ul class="view-nav clearfix"&gt; &lt;li class="view-nav-contents pull-left"&gt;&lt;a data-popview="" href="#nav"&gt;Previous&lt;/a&gt;&lt;/li&gt; &lt;li class="view-nav-next pull-right"&gt;&lt;a data-pushview="" href="#how"&gt;Next&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;div id="cstep2" class="view"&gt; &lt;ul id="step2"&gt; &lt;li&gt;&lt;a href="#" data-id="27" class="step"&gt;Test10&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#" data-id="28" class="step"&gt;Test11&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#" data-id="29" class="step"&gt;Test12&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>What I'm doing wrong? </p> <p><strong>UDATE</strong></p> <p>After read the docs you leave I made this changes to my jQuery code but still don't work:</p> <pre><code>$(function() { $('#categories-picker').simpleSlideView({duration: 250}); $("a.step").on("click", function() { var id = $(this).attr('data-id'); $.ajax({ type: 'GET', url: Routing.generate('category_subcategories', {parent_id: id}), dataType: "json", success: function(data) { var k = 2; if (data.length != 0) { $("#cstep1").after('&lt;div class="view" id="cstep' + k + '"&gt;&lt;ul id="step' + k + '"&gt;&lt;/ul&gt;&lt;/div&gt;'); var LIs = ""; $.each(data, function(index, value) { $.each(value, function(i, v) { LIs += '&lt;li&gt;&lt;a class="step" data-id="' + i + '" href="#"&gt;' + v + '&lt;/a&gt;&lt;/li&gt;'; }) }); $('#step' + k).html(LIs); k++; } } }); }); }); </code></pre>
    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.
 

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