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 ).