Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put HTML in jQuery .text()

Tags:

html

jquery

I couldn't find this anywhere, partly because its keywords are pretty common.

$('.pause_button').text('<img src="../imgs/icons/control_pause_blue.png" alt="Resume" /> Resume');

How can this be printed as HTML and not convert the brackets and apostrophes?

like image 428
Derek Avatar asked Jul 21 '10 06:07

Derek


People also ask

HOW include HTML in jQuery?

Step 1: Firstly, we have to open that Html file in which we want to add the jQuery using CDN. Step 2: After then, we have to place the cursor between the head tag just before the title tag. And, then we have to use the <script> tag, which specify the src attribute for adding.

What is text () in jQuery?

jQuery text() Method The text() method sets or returns the text content of the selected elements. When this method is used to return content, it returns the text content of all matched elements (HTML markup will be removed). When this method is used to set content, it overwrites the content of ALL matched elements.

How HTML elements are used in jQuery with example?

The html() method in jQuery is used to get the contents of the first element in the set of matched elements or is used to set the HTML contents of every matched element. It returns the content of the first matched element. This function does not accept any arguments.


2 Answers

Make use of the .html function avaialble in jQuery, .html().

For example:

$('div.demo-container').html('<p>All new content. <em>You bet!</em></p>');

In your case

$('.pause_button').html('<img src="../imgs/icons/control_pause_blue.png" 
alt="Resume" /> Resume');
like image 136
Pranay Rana Avatar answered Sep 22 '22 04:09

Pranay Rana


.text() actually escapes all HTML specific characters, .html() doesn't.

like image 28
muxare Avatar answered Sep 20 '22 04:09

muxare