javascript - After JS append a <li> in existing <ul>, i cannot selected it with click event -
this question has answer here:
i have dropdown list (bootstrap), after selected element in dropdown list, call php file ajax load specific content new dropdown list.
in js script append result found in existing ul list.
my problem after append new li element in ul list #submitpoint, cannot select them selector js. don't know why js seem not see new elements append before..
here php code 2 dropdown list, 1 flow name, other 1 flow's submit point:
print "<div class='row'>" ; print "<div id='flowspane' class='col-sm-6' style='display: true;'>" ; print "<div class='btn-group'>" ; print "<a id='dropdowntext1' class='btn btn-default dropdown-toggle btn-select' data-toggle='dropdown' href='#'>select flow <span class='caret'></span></a>" ; print("<ul id='flows' class='dropdown-menu'>"); foreach ($flows $flow) { print("<li id='" . $flow . "'>" . $client->getnameofflow($flow) . "</li>"); } print "</ul>" ; print "</div>" ; print "</div>" ; print "<div id='submitpointpane' class='col-sm-6' style='display: none;'>" ; print "<div class='btn-group'>" ; print "<a id='dropdowntext2' class='btn btn-default dropdown-toggle btn-select' data-toggle='dropdown' href='#'>select submitpoint <span class='caret'></span></a>" ; print("<ul id='submitpoint' class='dropdown-menu'>"); here js code :
<script type="text/javascript"> $(document).ready(function() { $("#flows li ").click(function(e){ var flow = $(this).text(); //window.alert(flow); document.getelementbyid('dropdowntext1').innerhtml = flow + ' <span class="caret"></span>'; }); $('#flows li').click(function(e) { $("#submitpointpane").show(); //window.alert(e.target.id); var post_url = "include/submitpoint_list.php"; $.ajax({ type: 'post', url: post_url, data: {'flowid': e.target.id}, datatype: 'json ', success: function(data) { $("#submitpoint").empty(); var ite = 0; while (ite < data.length) { $("#submitpoint").append("<li>" + data[ite] + "</li>"); //document.getelementbyid('submitpoint').innerhtml = "<li>" + data[ite] + "</li>"; ite++; } return false; }, error: function(xhr, status, error) { window.alert("error " + xhr.responsetext); } }); //return false; }); $("#submitpoint li").click(function(e){ var sp = $(this).text(); window.alert(sp); document.getelementbyid('dropdowntext2').innerhtml = sp + ' <span class="caret"></span>'; }); }); </script> so after append li element in ul #submitpoint, if click on it, #submitpoint li click function never call, li element appear in list.... (sorry cannot add picture)
do have ideas can me question why jquery selector not work new li element append? all!
the issue you're experiencing caused simple fact, dynamically created <li> item doesn't have listener click event attached.
what need use .on() method, attach event listener fod dynamically created elements.
// event listener being applied parent. $('#flows').on('click', 'li', callback);
Comments
Post a Comment