Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of document.createTextNode(' ') in jQuery

Tags:

jquery

What can I use in jquery instead of document.createTextNode(' ') js line.

I'm trying to do sth like this :

js : divButtons.appendChild(document.createTextNode(' '));

jquery : $("#divButtons").append(?????);

like image 245
penguru Avatar asked Aug 05 '10 06:08

penguru


People also ask

What does document createTextNode do?

createTextNode() Creates a new Text node. This method can be used to escape HTML characters.

What is Textnode in Dom?

HTML DOM Document createTextNode()Attributes are nodes. Texts are nodes. Some elements contain other nodes. Some elements contain text nodes. Some elements consain attribute nodes.

Which of the method creates a text node with the specified text?

The HTML DOM createTextNode() method is used to create a Text Node with the specified text.


1 Answers

Please be careful with the existing answers. They show how to append HTML. HTML is not Text. Treating text as HTML is a) a bug and b) dangerous.

Here is a sane solution:

$('selector').append(document.createTextNode('text < and > not & html!')) 

This is using jQuery only for the append part. Nothing wrong with using createTextNode. jQuery uses createTextNode internally in its text(str) function.

like image 133
usr Avatar answered Sep 23 '22 00:09

usr