0

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>
Vanquished Wombat
  • 9,075
  • 5
  • 28
  • 67
  • 1
    `The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit`: [charCodeAt()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt). In contrast keyCode properties are decimal representations of the ASCII value. – Rory McCrossan Sep 28 '17 at 10:29
  • @RoryMcCrossan ok so looking at http://www.fileformat.info/info/unicode/char/2060/index.htm, HTML Entity ⁠ = Unicode 8288 = UTF-8(hex) 0xA0. – Vanquished Wombat Sep 28 '17 at 10:35
  • @RoryMcCrossan So this answer by the venerable T.J.Crowder should work for me ?https://stackoverflow.com/questions/37387414/trim-nbsp-values-in-javascript – Vanquished Wombat Sep 28 '17 at 10:44
  • 1
    What about just using the `replace()` function? https://jsfiddle.net/ova2Le06/ – Gavin Thomas Sep 28 '17 at 10:47
  • @GavinThomas - thanks that work in my simple example though a more complex case where there were embedded nbsp chars would see them replaced too. The trim() function is means to operate on space chars only at beginning and end of the string. If you would care to modify to cater for this (maybe replace(/\s+/, " ").trim() ?) and present as an answer.... – Vanquished Wombat Sep 28 '17 at 10:58
  • 1
    Id suggesting using `var str = $.trim($( "#div1" ).text().replace(/\s+/, ""));`, but if you have a more complex example I'd be happy to take a look? – Gavin Thomas Sep 28 '17 at 11:05
  • @GavinThomas - nothing specific in terms of a more complex case, I am just generally wary or replacing more than intended in what really needs to be a trim replacement. My variation of your theme appears to work, btw. – Vanquished Wombat Sep 28 '17 at 11:16

0 Answers0