Due to circumstances out of my control, SharePoint, I have the following piece of code.
var item = $('<span><font size=1> </font></span>').text()
I am trying to compare the .text()
value to
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/
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.
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.
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.
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.
Try checking for '\xa0'
(which is the character created by
):
var item = $("<span><font size=1> </font></span>").text();
alert("'" + item + "' " + (item == '\xa0'));
http://jsfiddle.net/hUBeP/3/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With