Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

innerHTML in Combination with parseFloat

What I'm attempting to do can be accomplished by the following...

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

or even by...

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

but I can't help to wonder if there's a more elegant way to retrieve and assign DOM content as a float that I may be unaware of. Any insight?

like image 232
65Fbef05 Avatar asked Dec 21 '22 14:12

65Fbef05


1 Answers

There is the unary plus operator which tries to convert a string (or another type's toString()) to a number. It would be used like:

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

As others have mentioned you can use jQuery as essentially syntactic sugar for .innerHTML here, also.

like image 77
justkt Avatar answered Jan 05 '23 04:01

justkt