19 users online (0 members and 19 guests)  


  Results 1 to 3 of 3

Related

  1. Beginner CSS Help ?    Forum: CSS Forum
    Replies: 4
  2. help! html beginner    Forum: HTML Forum
    Replies: 1
  3. Best Places to Start, Help a Beginner    Forum: HTML Forum
    Replies: 5
  4. beginner here! this should be ez    Forum: HTML Forum
    Replies: 10
  1. #1
    proctk's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2006
    Posts
    5

    Beginner needs help

    Hi,

    I just started learning this javaScript two days ago and have very little knowledge of HTMl.

    I have created the below. its a simpile form that is supposed to add two values. can get it to work. I'm guessing something simple

    any help is great

    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title>test</title>
        <script language='Javascript'>
        <!--
        function computeForm(form) {
        
        var x = eval(form.first.value);
        var y = eval(form.second.value);
        form.total.value = eval(x) + eval(y);
        }
    
    </script>
    </head>
    
    <body>
    
    <form method="POST">
    
    First <INPUT type="text" name="first" size=30 maxlength=30 >
     <p>
    Second <INPUT type="text" name="second" size=30 maxlength=30>
     <p> 
    Total <INPUT type="text" name"total" size=30 maxlength=30 > 
    <p>
    <INPUT type="button" name="myButton" value="A Button" onClick="computeForm(this.form)">
    </form>
    </body>
    </html>

  2. #2
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Beginner needs help

    Hi there proctk,

    and a warm welcome to these forums.

    try it like this..
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd"> 
    <html> 
    <head>  
    <title>test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      
    <script type='text/javascript'>  
    <!-- 
    function computeForm(form) { 
    
      var x=parseFloat(form.first.value); 
      var y=parseFloat(form.second.value); 
    
    if((isNaN(x))||(isNaN(y))) {
      alert('numbers only please');
      form.reset();
      form.first.focus();
      return;
     }
      form.total.value=x + y; 
    } 
    //-->
    </script> 
    
    <style type="text/css">
    <!--
    fieldset {
        width:234px;
        padding:10px;
        border:3px double #000;
        margin:20px auto;
     }
    div {
        width:224px;
     }
    label,input {
        display:block;
        width:100px;
        float:left;
        margin:5px 2px;
     }
    -->
    </style>
    
    </head> 
    <body onload="document.forms[0].first.focus()"> 
    
    <form action="#"> 
    <fieldset>
    <legend>Total Test</legend>
    
    <div>
    <label>First :</label>
    <input type="text" name="first">
    </div>
    
    <div>
    <label> Second :</label>
    <input type="text" name="second"> 
    </div>
    
    <div>
    <label> Total :</label> 
    <input type="text" name="total" readonly="readonly">
    </div>
    
    <div> 
    <input type="button" value="Calculate" onclick="computeForm(this.form)">
    <input type="reset" value="Reset" onclick="this.form.first.focus()">
    </div>
     
    </fieldset>
    </form>
    
     </body> 
    </html> 
    I would, also, suggest that you avoid the use of 'eval', considered by many to be 'evil'.
    Last edited by coothead; 02-26-2006 at 06:46 AM. Reason: forgot to insert readonly for total

  3. #3
    proctk's Avatar
    New User

    Status
    Offline
    Join Date
    Feb 2006
    Posts
    5

    Re: Beginner needs help

    thank you for the reply, from what you posted, I have lots to learn, thank yor for the tip



Tags for this Thread