Calling up html text box using javascript and converting it to an integer -


so want understand how this, simple example:

my html

<div id = "pie"> eee </div>  <input type="text" name="item1" size="5"> 

and javascript is

var q = parsefloat("10");  document.getelementbyid("pie").innerhtml = q ; 

and want call whatever text in text box (named item1) , convert integer, display number. i'm sure there's easy way cant figure out

try this:

+(document.getelementsbyname('item1')[0].value); 

that shortest way display it, example in element #pie

document.getelementbyid('pie').innerhtml = +(document.getelementsbyname('item1')[0].value) 

putting + before convert number. if text box isn't number can detect by:

var value = document.getelementsbyname('item1')[0].value;  document.getelementbyid('pie').innerhtml = !isnan(value)?+value:"not number"; 

demo


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -