Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last line of HTML with Javascript

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"?

like image 270
Joe Avatar asked Mar 23 '11 03:03

Joe


People also ask

How do you go to the last line in HTML?

To do a line break in HTML, use the <br> tag.

How do you exit a line in JavaScript?

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.


2 Answers

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.

like image 171
Reid Avatar answered Oct 28 '22 13:10

Reid


$('#your_span').html().split(/\r?\n/).pop()

https://www.w3schools.com/jsref/jsref_pop.asp

like image 21
pcw11211 Avatar answered Oct 28 '22 13:10

pcw11211