i am creating new list elements dynamically. When clicking these elements I want execute code.
The event $("li").click()
only fires for my default list item <li>I am a default list element</li>
but not for any newly created ones. Please see my code below:
<script type="text/JavaScript">
$(document).ready(function() {
console.log('document.ready callback');
$("input#input2").click( function() {
console.log('input#input2.click: ' + $("input#input1").val() + ' added to list');
$("ul").append('<li>'+$("input#input1").val()+'</li>');
});
$("li").click( function() {
console.log('li.click: ' +$(this).text());
});
// Info
// .on('click', function()
// .live('click', function()
// did not solve this problem
});<script><html>
<body>
Enter some stuff: <input type="text" id="input1" value="stuff">
<br>
<input type="submit" id="input2" value="Submit stuff to List">
<p>stuff list!</p>
<ul>
<li>I am a default list element</li>
</ul>
</body>
I found a similar question here but unfortunately that did not solve my current case.
thx y'all!!