Help programming Javascript button

Discussion in 'JavaScript' started by graemeh88, Jul 21, 2010.

  1. #1
    Hi all, im very new to javascript, and am trying to program a button (Go for it) to execute a function (calculate), but it isnt working. If anyone could take a look at my code, and give any pointers or help, it would be much appreciated. I made notes in the code saying what im trying to do, and please ask if you need clarification.

    CODE

    <html>
    <head>
    <title>E-Marks</title>

    <script language = "JavaScript">

    \\ Set variables for program

    function calculate()
    {
    var student_id,
    course_number,
    description,
    distance_education,
    // course_count = 0,
    // a,
    // b,
    // c,
    // course_cost = 0,
    additional_marks,
    // total_courses,
    total_cost;
    // new_total_cost;

    student_id = parseInt(document.form_request.student_id.value);
    course_number = parseInt(document.form_request.course_number.value);
    description = parseInt(document.form_request.description.value);
    distance_education = parseInt(document.form_request.distance_education.value);
    additional_marks = parseInt(document.form_request.additional_marks.value);
    //a=10;
    //b=0;
    //c=0;
    //total_courses=0;
    //total_cost=0;

    //Fill Summary Text Area

    if (additional_marks <= 4)
    {
    total_cost = additional_marks*5;
    }
    document.form_request.total_cost.value = total_cost;


    //document.form_request.display.value = document.form_request.display.value + "Student I.D.: " + student_id + "\n";
    //}
    //document.form_request.display.value = document.form_request.display.value + course_count;
    //}
    //document.form_request.display.value = document.form_request.display.value + course_number;
    //}
    //document.form_request.display.value = document.form_request.display.value + description;
    //}
    //document.form_request.display.value = document.form_request.display.value + additional_marks;
    //}
    //document.form_request.display.value = document.form_request.display.value + distance_education;
    //}
    //document.form_request.display.value = document.form_request.display.value + course_cost;
    //}
    //document.form_request.display.value = document.form_request.display.value + total_cost;
    //}




    // Calculate Total number of courses requested


    // Calculate Total cost of marks requested for all courses

    //while(additional_marks < 5)
    //{
    //total_cost=additional_marks*5;
    //document.form_request.total_cost = total_cost;
    //}
    //while(additional_marks >5 and additional_marks <10)
    //{
    //total_cost=


    // Calculate extra costs for Distance education


    //if(document.form_request.distance_education.checked) { total_cost = total_cost+50; }

    // Prepare graph for Danger Range

    //if(a==10)
    //{
    //document.danger_range.process.value = document.danger_range.process.value + "Danger Range";
    //document.danger_range.process.value = document.danger_range.process.value + "\n" + a;
    //for(i = 0; i < total_cost; i++)
    //{
    //document.danger_range.process.value = document.danger_range.process.value + "*";
    //} a++;
    //} else{
    //document.danger_range.process.value = document.danger_range.process.value + "\n" + a;
    //for(i = 0; i < total_cost; i++)
    //{
    //document.danger_range.process.value = document.danger_range.process.value + "*";
    //}
    //a++;
    //}


    // Identify error condition for cost > $250

    //if (total_cost>250) { alert('Request Denied - Clear and Reset'); }


    // Set conditions and extra costs for radio button selections

    //if (document.form_request.doctors_note.checked==true) { total_cost=total_cost } if
    //(document.form_request.lack_of_work.checked==true) { total_cost=total_cost*1.2 } if
    //(document.form_request.some_compassion.checked==true) { total_cost=total_cost*1.05 }


    // Determine discount based on check boxes

    //if (document.something.first_time_user.selected==true) { alert ('Thanks for using E-Marks') } if
    //(document.something.high_flier.selected==true) { total_cost=total_cost*0.75 } if
    //(document.something.buying_a_degree.selected==true) { total_cost=total_cost*0.5

    }

    // Create form for website

    </script>

    </head>

    <body> <u><h1>E-Marks</h1></u> <br></br>

    <b><font size=+1>The Request</font></b>

    <form name = 'form_request'>
    <table border = "3">
    <tr><td>I.D.</td><td><input name ='student_id' name = "I.D." type = "text" size="20"> </td> </tr>
    <tr><td>Course Number</td> <td> <input name ='course_number' name = "Coursen Number" type = "text" size="20"> </td> </tr>
    <tr><td>Description</td> <td> <input name ='description' name = "Description" type = "text" size="20"> </td> </tr>
    <tr><td>Distance Education</td> <td style='text-align: center'> <input name ='distance_education' name ="Distance Education" type="checkbox"> </td> </tr>
    <tr><td>Additional Marks</td> <td> <input name='additional_marks' name = "Additional Marks" type = "text" size="20"> </td></tr>
    </table>

    <br></br>

    Number of Courses <input name='number_of_courses' name = "Number of Courses" type = "text" size="20"
    onfocus="textChange('number_of_courses', '0');" onblur="textChange('number_of_courses', '0');" value="0" />
    Total Cost <input name='total_cost' name = "Total Cost" type = "text" size="20"
    onfocus="textChange('total_cost', '0');" onblur="textChange('total_cost', '0');" value="0" />

    <br></br>

    <b>Reasons:</b>
    Doctor's Note <input id='doctors_note' name = "things" checked type = "radio" >
    Lack of Work <input id='lack_of_work' name = "things" type = "radio" >
    Some Compassion <input id='some_compassion' name = "things" type = "radio" >

    <br></br>

    <input type ="button" value ="Go For It" onclick = "calculate()">

    <input id='clear_and_reset' type = "button" value = "Clear And Reset" onclick = "reset()"> <select id="something"> <option id='first_time_user'value
    ='First Time User'>First Time User</option> <option id='frequent_flier' value ='Frequent Flier'>Frequent
    Flier</option> <option id='buying_a_degree' value ='Buying a Degree'>Buying a Degree</option> </select>

    <br></br>

    <span style='font-size: 20px; font-weight: bold'>Summary of Your Request(s)</span>

    <br></br>

    <textarea id='summary_of_your_request' name="comments" rows = "10" cols="30"></textarea>

    <br></br>

    <span style='font-size: 20px; font-weight: bold'>Danger Range</span><br></br> <textarea id='danger_range'
    name="comments" rows = "10" cols="30"></textarea> </form>

    </body> </html>
     
    graemeh88, Jul 21, 2010 IP
  2. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #2
    There's a syntax error here:
    <input type ="button" value ="Go For It" onclick = "calculate()"> 
    HTML:
    You forgot to add ";" after calculate().

    Open it with Firefox, click Tools, then Error Console. All javascript errors and warning messages are there, including which line it is.
     
    Rainulf, Jul 21, 2010 IP