var str='Hello World'; var tooltipHtml = '<b>' + str + '</b>'; $('#tooltip').html(tooltipHtml); 

The result in the #tooltip container is the word Hello World. But if

 var str='<h1>Hello World</h1>'; 

The result will be

Hello world

but <h1>Hello World</h1> is an example, the tags will be different in different situations, how to fill a container (in this example tooltip) and html and text at the same time? Can somehow escape all tags in str?

The second option that can occur is when

 var str='Hello World'; var tooltipHtml = '<b>' + str + '<span class="error">Часть недоступна</span></b>'; $('#tooltip').html(tooltipHtml); 

    1 answer 1

    Try this

     var str='<h1>Hello World</h1>'; var container = $('<b></b>'); container.text(str); $('#tooltip').html(container); 
    • Thanks, but there is another option I added to the description of the problem. When our container, in addition to just str text, must contain a part of html <span class="error">Часть недоступна</span> and this part should be displayed as html. - Igor Baranyuk
    • So decided $('#tooltip').html(container.append('<span class="error">Часть недоступна</span>')); - Igor Baranyuk