" with "
  • " in an array in jQuery? Say I have it $(document).ready(function() { var array = $.makeArray($('p')); $(array...">

    How to replace "<p>" with "<li>" in an array in jQuery?

    Say I have it

    $(document).ready(function() {
       var array = $.makeArray($('p'));
       $(array).appendTo(document.body);
       });
    });
    

    <p>how</p>
    <p>are</p>
    <p>you</p>
    <p>baby?</p>
    

    If I want to replace <p>with <li>, and the expected result will be ...

    <li>how</li>
    <li>are</li>
    <li>you</li>
    <li>baby?</li>
    

    What should I do? Thanks in advance!

    +3
    source share
    2 answers
    $("p").each(function () {
         $(this).replaceWith("<li>" + $(this).html() + "</li>");
    });
    
    +8
    source

    Here is a quick and dirty solution, but if you give me more detailed information about what you are trying to do, we can come up with a better option.

    $('p').each(function(){$(this).replaceWith('<li>'+$(this).html()+'</li>')})
    
    +1
    source

    Source: https://habr.com/ru/post/1739107/


    All Articles