The jquery text() function gets the combined text contents of each element in the set of matched elements, including their descendants.
The jquery trim() function removes the whitespace from the beginning and end of a string.
So I should be able to use
var str = $("#div1").text()
to get the text from an html element and its contents, then
var str = $("#div1").text().trim()
However the text() function gets the text after the browser has replaced encoded characters and others with the char code equivalents.
Therefore calling .trim() on .text() will not replace white space. There are SO questions and answers that talk about regexp solutions targeting char code 160 to solve this issue. However, what I find confusing is that the charcode of the space is 8288 and not 160. What is going on - see snippet text area 2 showing 8288 as charcode for the space ?
var str = $( "#div1" ).text()
$( "#out" ).val( str );
$( "#out1" ).val( str.charCodeAt(0) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Below is the text sample</p>
<div id="div1"><p><span></span> The text sample</p></div>
<p>This textarea shows the result of .text() - note the first char is a blank</p>
<textarea id='out'></textarea>
<p>This textarea shows the char code of char 1 - was expecing 160!</p>
<textarea id='out1'></textarea>