Search code examples
jqueryparentthisblur

jQuery, $(this).parent() and blur()


I cannot get the blur() function in the following to work:

$('.newselect').focus(function(){
    $(this).parent().append('<div class="select_container"></div>');
});
$('.newselect').blur(function(){
    $(this).parent().remove('.select_container');
});

However if I use a universal selector $('*') (as below) it works, why is this and how can I fix the problem?

$('.newselect').focus(function(){
    $(this).parent().append('<div class="select_container"></div>');
});
$('.newselect').blur(function(){
    $('*').remove('.select_container');
});

Solution

  • Try this:

    $('.newselect').focus(function(){
        $(this).parent().append('<div class="select_container"></div>');
    }).blur(function(){
        $(this).siblings('.select_container').remove();
    });