Search code examples
jqueryreplacewithinsertafter

What is faster - replaceWith or insertAfter (and hiding old element) in jQuery


I want to change some elements on the page. (for example I'll change inputs to divs' structures).

What is faster? To do it with replaceWith(). Or to insert div structure after the input, and then hide input?


Solution

  • If you talking about something like an inline editing function I would use...

    replaceWith()
    

    Otherwise you'd be duplicating content on your page which may cause other problems.

    EDIT: In some cases I also use...

    .empty().html()
    

    All really depends on what you're trying to accomplish though.

    EDIT: Here is a fiddle based on your comment...

    http://jsfiddle.net/wdm954/WgjL5/2/

    I would still go with replaceWith() instead of having multiple instances of a button in your code.