Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Tjk
    Tjk is offline
    New Coder
    Join Date
    Aug 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Basic Javascript text box problem

    My first attempt at some Javascript....

    Code:
    <html>
    <head>
    <form>
    <script type="javascript">
    function test(){
      if ($input == 0){
        var $number= 0;
      }
       if ($input>0){
         var $number= window.document.all.input1.value;
       }
       window.document.form.output= $number;
    }
      
    </script
    </head>
    <body>
    <tr>
    <td><input type="text" size="10" name="input1" id="input1" value="0" onChange="test();"></td>
    </tr><br>
    <td><INPUT size=3 name=output id=output value="0"></td>
    </tr>
    
    </body>
    </form>
    </html>
    Basically I'm looking so that if I input a number into text box "input1", the Javascript will then write that number to text box "output" automatically using onChange.

    Can you tell me what is wrong with this please?

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,536
    Thanks
    3
    Thanked 513 Times in 500 Posts
    Code:
    <html>
    <head>
    <form>
    
    <script type="text/javascript">
    <!--
    
    function test(){
     number= document.getElementById('input1').value;
     if (number==''){
       number= '0';
     }
     document.getElementById('output').value= number;
    }
    
    function test2(){
     number= document.getElementById('input2').value;
     if (number==''){
       number= '0';
     }
     document.getElementById('output2').value= number;
    }
    
    function test3(){
     number= document.getElementById('input3').value;
     if (number==''){
       number= '0';
     }
     document.getElementById('output3').value= number;
    }
    //-->
    </script></head>
    <body>
    <table>
    <tr>
    <td><input type="text" size="10" name="input1" id="input1" value="0" onchange="test();"></td>
    </tr>
    <tr>
    <td><INPUT size=3 name=output id=output value="0"></td>
    </tr>
    <tr>
    <td><input type="text" size="10" name="input2" id="input2" value="0" onkeyup="test2();"></td>
    </tr>
    <tr>
    <td><INPUT size=3 name=output2 id=output2 value="0"></td>
    </tr>
    <tr>
    <td><input type="text" size="10" name="input3" id="input3" value="0" onblur="test3();"></td>
    </tr>
    <tr>
    <td><INPUT size=3 name=output3 id=output3 value="0"></td>
    </tr>
    </table>
    Basically I'm looking so that if I input a number into text box "input1",
     the Javascript will then write that number to text box "output" automatically using onChange.
    
    </body>
    </form>
    </html>

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Tjk

    I fixed your own script which might be easier to compare.
    Javascript variables do not use the dollar sign ($)

    PHP Code:
    <html>
    <
    head>

    <
    script type="text/javascript">
    <!--
    function 
    test(){

    myInput=document.forms["f1"]["input1"].value

    if (myInput == "" || myInput==" "){
    myOutput0
    }
    else{
    myOutputmyInput
    }

    document.forms["f1"]["output"].valuemyOutput
    }
     
     
    //-->
    </script>
    </head>
    <body>

    <form name="f1">

    <input type="text" name="input1" value="0" onchange="test()"><br>
    <input type="text" name="output" value="">

    </form>

    </body>
    </html> 
    Last edited by Mr J; 10-16-2005 at 03:40 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #4
    Tjk
    Tjk is offline
    New Coder
    Join Date
    Aug 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks both of you. It worked in the end!

    One last query for the day...

    If I want to keep a submit button hidden at the bottom of the script and then it only reveal itself when both of the input fields are filled in, how would I do this?

  • #5
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Check here: http://homepage.ntlworld.com/vwphill...Compendium.htm
    He's got lots of useful scripts like that. It's helped me a lot.

  • #6
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Give this a try

    PHP Code:
    <html
    <
    head

    <
    script type="text/javascript"
    <!-- 
    function 
    test(){ 

    myInput=document.forms["f1"]["input1"].value 

    if (myInput == "" || myInput==" "){ 
    myOutput

    else{ 
    myOutputmyInput 
    }

    document.forms["f1"]["output"].valuemyOutput


    if(chkNum(myInput)){
    document.forms["f1"]["sub"].style.visibility="visible"
    }
    else{
    document.forms["f1"]["sub"].style.visibility="hidden"
    alert("Please enter a number")
    }

    }

    function 
    chkNum(val){
    chk=parseFloat(val)
    if(
    chk==(val*1)){
    return 
    true
    }
    else{
    return 
    false
    }
    }
     
    //--> 
    </script> 
    </head> 
    <body> 

    <form name="f1"> 

    <input type="text" name="input1" value="0" onchange="test()"><br> 
    <input type="text" name="output" value="0"> 

    <input type="submit" name="sub" style="visibility:hidden">

    </form> 

    </body> 
    </html> 
    Last edited by Mr J; 10-16-2005 at 11:57 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #7
    Tjk
    Tjk is offline
    New Coder
    Join Date
    Aug 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Could you explain to me how this script works ie what each bit of the code does?
    Last edited by Tjk; 10-16-2005 at 11:08 PM.

  • #8
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Whos script?
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #9
    Tjk
    Tjk is offline
    New Coder
    Join Date
    Aug 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yours please.

  • #10
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    PHP Code:
    <html
    <
    head

    <
    script type="text/javascript"
    <!-- 
    function 
    test(){ 

    myInput=document.forms["f1"]["input1"].value // assign value to variable myInput

    if (myInput == "" || myInput==" "){ // if there is no value or there is only a single space
    myOutput// set variable myOutput to zero

    else{ 
    myOutput myInput // if there is a value then set variable outPut equal to myInput
    }

    document.forms["f1"]["output"].valuemyOutput // assign the form textbox the value of myOutput

    /* the following line sends the value of myInput to function chkNum which checks to see if it is a number*/
    if(chkNum(myInput)){
    document.forms["f1"]["sub"].style.visibility="visible" // if myInput is a number then show the button
    }
    else{
    document.forms["f1"]["sub"].style.visibility="hidden" // if not, hide then button
    alert("Please enter a number")
    }

    }

    function 
    chkNum(val){
    chk=parseFloat(val)
    if(
    chk==(val*1)){
    return 
    true
    }
    else{
    return 
    false
    }
    }

     
    //--> 
    </script> 
    </head> 
    <body> 

    <form name="f1"> 

    <input type="text" name="input1" value="0" onchange="test()"><br> 
    <input type="text" name="output" value="0"> 

    <input type="submit" name="sub" style="visibility:hidden">

    </form> 

    </body> 
    </html> 
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

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