Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching jquery .text() to  

Due to circumstances out of my control, SharePoint, I have the following piece of code.

var item = $('<span><font size=1>&nbsp;</font></span>').text()

I am trying to compare the .text() value to &nbsp; and don't know what to do. Stepping through the code item seems to equal " " which makes sense. But doing item == " " returns false. How should this comparison be done?

EDIT: Example fiddle

http://jsfiddle.net/hUBeP/2/

like image 649
anothershrubery Avatar asked Nov 07 '11 16:11

anothershrubery


People also ask

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 do I check if a div contains a text?

To check if a div element contains specific text:Use the textContent property on the element to get the text content of the element and its descendants. Use the includes() method to check if the specific text is contained in the div . If it is, the includes() method returns true , otherwise false is returned.

How do I get text inside a div using jQuery?

To get the value of div content in jQuery, use the text() method. The text( ) method gets the combined text contents of all matched elements. This method works for both on XML and XHTML documents.

How do I get inner text in jQuery?

Answer: Use the jQuery text() method You can simply use the jQuery text() method to get all the text content inside an element. The text() method also return the text content of child elements.


1 Answers

Try checking for '\xa0' (which is the character created by &nbsp;):

var item = $("<span><font size=1>&nbsp;</font></span>").text();
alert("'" + item + "' " + (item == '\xa0'));

http://jsfiddle.net/hUBeP/3/

like image 183
ThiefMaster Avatar answered Oct 18 '22 17:10

ThiefMaster