InnerHTML in combination with parseFloat

What I'm trying to do can be done as follows ...

elementContent = document.getElementById('docElement').innerHTML;
elementContent = parseFloat(elementContent);

or even...

elementContent = parseFloat( document.getElementById('docElement').innerHTML );

but I can't help but wonder if there is a more elegant way to get and assign DOM content as floating, which I may not know about. Any insight?

+3
source share
4 answers

There is a unary plus operator that tries to convert a string (or other type toString()) to a number. It will be used as:

elementContent = +document.getElementById('docElement').innerHTML;

As already mentioned, you can use jQuery as essentially syntactic sugar for .innerHTML here.

+8
source

. , , , HTML, "" , , , , HTML , . (, , "" Unicode , .)

, :

<span id='docElement' data-value='29.20221'>29.20221</span>

".innerHTML" ".getAttribute()":

var value = document.getElementById('docElement').getAttribute('data-value');
value = parseFloat(value);
+2

JQuery:

var html = parseFloat($('#docElement').html());
$('#docElement').html(html);
0

, ​​ jQuery, , :

var el = parseFloat( $('#docElement').text() );

Do not forget that you may encounter a problem when you need a trim()string.

0
source

All Articles