www.webdeveloper.com
Results 1 to 4 of 4

Thread: Javascript add Textbox input with string

  1. #1
    Join Date
    Jun 2012
    Posts
    2

    Smile Javascript add Textbox input with string

    Hi, this is a Addition question with a textbox and string.
    I want to use textbox input and var string, take what is in the textbox
    put it in the string and add it. then return the new number back into textbox which is Named txt.value as below.

    Code:
    <Input Type = Button NAME = b1 VALUE = "Add Numbers" onClick = calculate()>
    <input type="text" name="txt" id="txt" size="30" maxlength="70" Value="2">
    
    <script language="javascript" type="text/javascript">
    function calculate() {
    
    var Thestring =1 // add Thestring + txt.value to get 3
    
    holdstring = txt.value+Thestring
    txt.value = holdstring  // now put the new number in txt.value
    
    
    }
    </SCRIPT>

    The new number in txt.value should be 3 but its wrong
    now it shows 12. I don't remember how to add string and textbox numbers?
    any help would be appreciated...Thanks in Advance!

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,637
    holdstring = new Number(txt.value)+Thestring
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Jun 2012
    Posts
    2
    Code:
    holdstring = new Number(txt.value)+Thestring  // your code
    txt.value = holdstring  // new number
    
    --
    is this right?.
    
    holdstring  = new Number(txt.value)-Thestring
    Could you tell me about subtracting it too...
    I want to use subtract button the new number txt.value.

    Thank You

  4. #4
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,637
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>...</title>
    <style type="text/css">
    body{text-align:center;font-family:Georgia,Verdana,Arial;font-size:13px;background-color:#fff;color:#000;padding:200px 0px 0px 0px;margin:0px 0px 0px 0px;}
    input{text-align:center;}
    hr{width:45&#37;;color:Lightsteelblue;}
    </style>
    <script type="text/javascript">
    function calc(){
    var frm=document.math_form,
    a=new Number(frm.a.value),
    b=new Number(frm.b.value);
    frm.add.value=a+b;
    frm.substract.value=a-b;
    }
    </script>
    </head>
    <body>
    <form name="math_form" action="">
    <table align="center" cellspacing="3" cellpadding="3" border="0" width="500">
    <tbody>
    <tr><td><input type="text" name="a" /></td><td><input type="text" name="b" /></td></tr>
    <tr><td colspan="2"><hr /><input type="button" value="Calculate" onclick="calc()"  /><hr /></td></tr>
    <tr><td><b>ADD</b><br /><br /><input type="text" name="add" /></td><td><b>SUBSTRUCT</b><br /><br /><input type="text" name="substract" /></td></tr>
    </tbody>
    </table>
    </form>
    </body>
    </html>
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles