Question

My code:

 $('.menu a').on('click', function(event){
    event.preventDefault();
    if($(this).parent().children('ul').is('.deep1')){
        var result = $(this).parent().children('ul').html();
        $('.dpp1').html('<ul class="menu" style="display: none">'+result+'</ul>');
        $('.dpp1').children('.menu').stop().fadeIn()
    }else if($(this).parent().children('ul').is('.deep2')){
        var result = $(this).parent().children('ul').html();
        $('.dpp2').html('<ul class="menu" style="display: none">'+result+'</ul>');
        $('.dpp2').children('.menu').stop().fadeIn()
    }else if($(this).parent().children('ul').is('.deep3')){
        var result = $(this).parent().children('ul').html();
        $('.dpp3').html('<ul class="menu" style="display: none">'+result+'</ul>');
        $('.dpp3').children('.menu').stop().fadeIn() 
    }
 });

My question is how to keep it working preventDefault() in created elements?

Was it helpful?

Solution 2

Try this,

$(document).on('click', '.menu a', function (event) {
    event.preventDefault();
    // your code
});

OTHER TIPS

Since the anchors are added dynamically, you need to use event delegation to register the event handler

$('.menu').on('click', 'a', function (event) {
    event.preventDefault();
    //........
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top