Hi if have the following PHP-Code:
if (isset($_SESSION["kriterien"]) && count($_SESSION["kriterien"]) != 0) {
echo "<ul>" ;
foreach ($_SESSION["kriterien"] as $filterkriterumkey => $filterkriterium) {
foreach($filterkriterium as $filterkey => $filter) {
echo '<li><p>'.$filterkriterumkey.': '.$filter.'</p><button type="button" class="buttondeletefilter" id="'.$filterkriterumkey.'" name="'.$filterkey.'"><img src="img/delete.png" alt="" /></button></li>';
}
}
echo "</ul>";
}
As you see i generate al HTML-List with all Elements in the specific Session Element. Each time a Session Element is added i reload the code with $("#listdiv").load('list.php');
(I know its maybe not best practice). Which Jquery Event i have to use to do something if a new added button is clicked?
$(document).ready(function() { $(".buttondeletefilter").click(function(){ }); });
didnt worked.
EDIT:
i just reload script the too now. its not perfect cause i like to have one file for the scripts but it works
You need to bind the click
event to the dynamically created elements with:
$(document).on("click",".buttondeletefilter", function(){
alert($(this).attr('id'));
});
Use .on()
:
$(document).ready(function() {
$(".buttondeletefilter").on("click", function(){
});
});
hey you need to use on but need to do a delegation.you got two options
1.Either choose any static parent of the buttons for e.g. suppose you are buttons are in some static div called my div and you are not replacing it.
<div class="mydiv"> dynamic buttons here ...... </div> $("mydiv").on('click','.buttondeletefilter',function(){ your code goes here... })
2.If you dont have any static parent then you can choose document or body like this
$(document).on('click','.buttondeletefilter',function(){ your code goes here }) $('body').on('click','.buttondeletefilter',function(){ your code goes here })
I would always prefer any static parent for performance purpose.
This will definite work for you . ;)