In a variable string (html code) how to convert its contents and then paste into a document?

The following is a sample code, as I tried to do it

var html = '<ul><li>1</li><li>2</li><li>3</li></ul>'; $(html).find('li').each(function(i, li) { $(html).find('li').eq(i).text('Какой то текст'); }); $('body').append(html) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> 

    1 answer 1

     var html = '<ul><li>1</li><li>2</li><li>3</li></ul>'; var $html = $(html); $html.find('li').each((i, el) => $(el).text('Какой-то текст - ' + i + ", old: " + $(el).text())); $('body').append($html) 
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> 

    • In vain you have added arrow functions)) managed to copy well)) - Evgeny Shevtsov
    • @ YevgenyShevtsov Remaking the switch function to the traditional one, if it does not use this and the like, is trivial. - Igor
    • @YevgeniyShevtsov, if you don’t use a task / collector, then you can convert a modern JS code into an old and dull one, on the page babeljs.io/repl :) - yar85