How can I get the last line of HTML text with Javascript?
For example, a HTML request returns this response:
<span>blah blah blah
ha ha ha ha
tatatatata
How would I get the last line "tatatatata"?
To do a line break in HTML, use the <br> tag.
The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.
var html = document.getElementById('some_element').innerHTML.split(/\r?\n/);
alert(html[html.length - 1]);
Split by /\r?\n/
to break the HTML into lines, then grab the last element of the array.
Note: since this is HTML, you may want to split by /<br(?: \/)?>/
or /<br>/
, depending on the situation.
$('#your_span').html().split(/\r?\n/).pop()
https://www.w3schools.com/jsref/jsref_pop.asp
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