简体   繁体   中英

How do i get the length of innerHTML (total characters) in JavaScript?

I'm trying to get the length of the characters that I attain from innerHTML and it's kind of come down to last resort, but can't seem to find a more efficient way as the data is inside a slider that I'm trying to use to get the lower value and higher value.

var lvalue=document.getElementById("lval").innerHTML;

then I'm spiting the string in the spaces:

var larr=lvalue.split(" ");

The innerHTML value is something like this "2413dsk 134dfa134".

And when i use larr[0].length , I get 1 when I need 7. Is there a solution?

I think it would go something like this:

var lvalue = document.getElementById("lval").innerHTML;
var larr = lvalue.split(' ');
var len = 0;

// For each iterates over the index of arrays
for(var i in larr) { 
     len += larr[ i ].length // Acummulate the length of all the strings
}

Or alternatively you could count the spaces first and then substract it from the total length.

// Some people argue about extending the native objects
// but in this case I think this method is a natural fit.
String.prototype.count = function( character ) {
   var l = 0;
   // `this` refers to the string itself
   for(var c in this) n = this[ c ] === character ? ++n : n;
   return n;
}

An then use it like so:

var lvalue = document.getElementById("lval").innerHTML;

// Subtract total length minus the number of spaces
var len = lvalue.length - lvalue.count(' ');

This might be caused by a preceding or leading space.

Try trimming the extra spaces :

var lvalue=document.getElementById("lval").innerHTML.replace(/^\s+/gi,'').replace(/\s+$/gi,'');

var larr=lvalue.split(" ");

If there is someone like me that doesn't want to use JQuery, here is a example in javascript (only)...

The files

The html file...

<!-- start body -->
<body>
  <!-- start section -->
    <section id="bar"></section>
  <!-- end section -->
</body>
<!-- end body -->

The javascript file...

/* start - wut element do ya wanna get? */
foo = document.getElementById('bar');
/* end - wut element do ya wanna get? */

/* start function */
function isEmpty(){
  if (foo.innerHTML.length === 0) {
      /* your code here */
  }
}
/* end function */

What I did

In the HTML

Quite simple, I just created a section and assigned an ID on it. This ID will be used to call her in our javascript.

In the JavaScript

In the var fooI I called the section whose I gave an ID. After it I created a function that "do something" if the length of a element is equal zero.

Considerations

It works, but if you have a space or a line break the code will not consider it as "empty"...

I didn't see any problem with your code. I run a test here: http://desdegdl.com/lval.html Probably, you have one or more spaces at the begining of lval 's inner HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM