Without going into why it is needed: this function replaces your own tags (as, indeed, any existing tags whose name is specified in the parameter) with a span with the required class:

 function convertToHTML(tag){ $(tag).replaceWith(function(){ return '<span class="'+tag+'">'+$(this).text()+'<span>'; }); } 

I made this function based on this ( source ):

  $(".l2 .item").replaceWith(function(){ return ' <li class="item" > '+$(this).text()+', я кому сказал! </li>'; }); 

Is it possible to remove nested from my function, thus improving the code? (If you want to give an answer in jsfiddle , here is the working code ).

    1 answer 1

     $(tag).replaceWith('<span class="'+tag+'">'+$(tag).text()+'<span>'); 

    Update

    I am sorry, the replacement is not equivalent.

    If the $(tag) includes more than one element, then $(tag).text() is the glued innerText all of them. While replaceWith with a function calls this function separately for each replaced element in the context ( this ) of this element.

    • Thank you for your reply! - Hokov Gleb
    • It's my pleasure. Successes in programming. - Igor