I’ve got a little issue here.
What I’m trying to do is pass some parameters from html to javascript. Here is the code:
HTML:
<div id="percentage">52.338</div>
JAVASCRIPT:
var pctg = document.getElementById("percentage");
alert(pctg);
But it doesn’t work well. Could u tell me how to fix it? I’ve tried ‘parseFloat’ and some related things, but cannot work it out yet… Thx in advance.
What you are looking for is the text content of the element instead of the element itself. Some browsers (older IEs of course) use
innerTextinstead oftextContentso it’s a good idea to use both properties:If you actually want a number instead of just a string use
parseFloat()on the string.Depending on what you are doing it might be more advisable to store the value in a
data-attribute though. Then you can simply use.getAttribute('data-whatever')to access it instead of having to deal with getting the text content of the element. In case you use jQuery it’d become even nicer as you could then use.data('whatever')which would already return a number. Modern browsers also support direct access to data attributes via thedatasetproperty; but you probably don’t want to use this as it would break with older browsers.