madhur madhur - 1 year ago 76
Javascript Question

Getting number of lines in a div

I am trying to find number of lines in a div like this jsfiddle, but instead of returning 5 it is giving me 9. Any idea what is wrong here

var lines = document.getElementById('ptest').textContent;

P.S. - I also want to use the code to read the num of lines in a paragraph

Answer Source

try filter(String) to remove empty arrays

var len=document.getElementById('ptest').textContent.split(/\n/).filter(function(a){ var b = a.replace(/ /g,''); return (typeof(b) == 'string' &&  b != '')}).length

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download