jqueryreplacewith

jQuery wrap an element after replaceWith method but not work


I tried to wrap a div after replace the original element from div to input but not work, could anyone suggest what wrong with my code below, thanks.

$('.sth').replaceWith(
    $('<input>', {
        value: $('.sth').text(),
        type: 'text'
    })
).wrap('<div class="new" />');

Demo: http://jsfiddle.net/zhmUK/1/


Solution

  • http://api.jquery.com/replaceWith/

    Replace each element in the set of matched elements with the provided new content and return the set of elements that was removed.

    $('.sth').replaceWith(
        $('<input>', {
            value: $('.sth').text(),
            type: 'text'
        }).wrap('<div class="new" />').parent()
    );