jquery filtering has + not

You can use a combination of the :not and :has selectors like this

$("a.add-span").click(function() {
    $("li:not(:has(span))").each(function(index) {
        $(this).append('<span class="spn">new span<\/span>');
    });
});

Here is a demo http://www.jsfiddle.net/xU6fV/

Leave a Comment