I have two simple jQuery functions, one standard dropdown function and another function that appends HTML content on click. The problem is that the dropdown function does not work with the appended HTML.
Here's my HTML:
<a href="#" id="new_line">New Line</a>
<ul class="lines">
<li>
<a href="#" class="dropdown-toggle">Popup</a>
<ul class="dropdown-menu" style="background: #000; padding: 10px; display:none;">ddd
</ul>
</li>
</ul>
And the jQuery functions:
$(function() {
$('.dropdown-toggle').click(function(){
$('.dropdown-menu').not($(this).next('.dropdown-menu')).hide();
$(this).next('.dropdown-menu').toggle();
});
});
$(document).ready(function() {
var wrapper = $(".lines");
var add_button = $("#new_line");
$(add_button).click(function(){
$(wrapper).append('<li><a href="#" class="dropdown-toggle">Popup</a> <ul class="dropdown-menu" style="background: #000; padding: 10px; display:none;">ddd</ul></li>');
});
});
Please check the following example on codepen. You'll see one "dropdown" link which works. Once you click on "New Line" new dropdown links will appear, but those do not work. Any idea how to make them work?
Thanks!