www.webdeveloper.com
Results 1 to 3 of 3

Thread: [RESOLVED] Fill in Values from last value inputted

  1. #1
    Join Date
    Oct 2010
    Posts
    45

    resolved [RESOLVED] Fill in Values from last value inputted

    I am trying to get a multiple row/column form table to fill down the last value inputted in a textbox on a column to the rest of the textboxes in that column when a button or checkbox is selected. I am a total novice at JavaScript and have only been able to do a simple example but it's hardcoded to only use the value from the first textbox, not the last textbox that had a value. And I'm not sure how to accomodate multiple columns with this feature.

    Any help would be great.

    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    var grade1 = "";
    var grade2 = "";
    var grade3 = "";
    var grade4 = "";
    var grade5 = "";
    var grade6 = "";
    /*  Save the inputted grades so far */
    function InitSaveVariables(form) {
    grade1 = form.grade1.value;
    grade2 = form.grade2.value;
    grade3 = form.grade3.value;
    grade4 = form.grade4.value;
    grade5 = form.grade5.value;
    grade6 = form.grade6.value;
    }
    /* if checked, fill down from grade1 else unchecked return values to previous value */
    function FillDown(form) {
    if (form.copy.checked) {
    InitSaveVariables(form);
    form.grade1.value = form.grade1.value;
    form.grade2.value = form.grade1.value;
    form.grade3.value = form.grade1.value;
    form.grade4.value = form.grade1.value;
    form.grade5.value = form.grade1.value;
    form.grade6.value = form.grade1.value;
    }
    else {
    form.grade1.value = grade1;
    form.grade2.value = grade2;
    form.grade3.value = grade3;
    form.grade4.value = grade4;
    form.grade5.value = grade5;
    form.grade6.value = grade6;
       }
    }
    </script>
    
    </head>
    <body>
    <center>
    <form method="post" action="" name="testform">
    <table border="1" cellspacing="0" cellpadding="3" width="400">
    
    <tr bgcolor="#003399">
    <td colspan=3 width="100%" bgcolor="#003399">
    <b><font color=white size="-1" face="arial, helvetica">Fill Down Test</font></b>
    <font color=white size="-2" face="arial, helvetica">
    (Check to use Fill Down: <input type="checkbox" name="copy"
    OnClick="javascript:FillDown(this.form);" value="checkbox"> )</font>
    </td>
    </tr>
    <tr>
        <td>Student</td><td>Test 1</td><td>Test 2</td>
    </tr>
    <tr>
    <td>
    <font size="-1" face="arial, helvetica">Student 1:</font>
    </td>
    <td>
    <input type="text" size="5" name="grade1">
    </td>
    <td>
    <input type="text" size="5" name="grade1a">
    </td>
    </tr>
    <tr>
    <td>
    <font size="-1" face="arial, helvetica">Student 2:</font>
    </td>
    <td>
    <input type="text" size="5"name="grade2">
    </td>
    <td>
    <input type="text" size="5"name="grade2a">
    </td>
    </tr>
    <tr>
    <td>
    <font size="-1" face="arial, helvetica">Student 3:</font>
    </td>
    <td>
    <input type="text" size="5" name="grade3">
    </td>
    <td>
    <input type="text" size="5" name="grade3a">
    </td>
    </tr>
    <tr>
    <td>
    <font size="-1" face="arial, helvetica">Student 4:</font>
    </td>
    <td>
    <input type="text" size="5" name="grade4">
    </td>
    <td>
    <input type="text" size="5" name="grade4a">
    </td>
    </tr>
    <tr>
    <td>
    <font size="-1" face="arial, helvetica">Student 5:</font>
    </td>
    <td>
    <input type="text" size="5" name="grade5">
    </td>
    <td>
    <input type="text" size="5" name="grade5a">
    </td>
    </tr>
    <tr>
    <td>
    <font size="-1" face="arial, helvetica">Student 6:</font>
    </td>
    <td>
    <input type="text" size="5" name="grade6">
    </td>
    <td>
    <input type="text" size="5" name="grade6a">
    </td>
    </tr>
    
    </table>
    </form>
    </center>
    
    </body>
    </html>

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Fill Down Test</title>
    
    <script type="text/javascript">
    window.onload=function() {
    var aObj = document.getElementsByTagName('input');
    var i = aObj.length; 
    while(i--) { 
        if(aObj[i].type=='text') {
        	aObj[i].onchange = function() {down[this.parentNode.cellIndex] = this.parentNode.parentNode.rowIndex;};
            }
        else {
            aObj[i].onclick = function() {fillDown(this);};
            }
        }
    };
    
    var down = [];
    function fillDown(obj) {
    var aObj = document.getElementsByTagName('tr');
    var len = aObj.length; 
    var col = obj.parentNode.cellIndex;
    var val = aObj[down[col]].getElementsByTagName('input')[col-1].value;
    for(var i=down[col]; i<len; i++) {
        aObj[i].getElementsByTagName('input')[col-1].value = val;
        }
    };
    </script>
    
    <style type="text/css">
    body {
    font-family:Arial, Helvetica, sans-serif;
    font-size:0.8em;
    color:#000;
    background:#fff;
    }
    table {
    border:1px solid;
    border-collapse:collapse;
    width:400px; 
    margin:auto;
    }
    th {
    color:#fff;
    background:#039;
    }
    th, td {
    border:1px solid;
    padding:3px;
    }
    input[type='button'] {
    font-size:smaller;
    }
    </style>
    
    </head>
    <body>
    <form method="post" action="#" name="testform">
    <table>
    <thead>
        <tr>
            <th colspan="3">Fill Down Test</th>
        </tr>
        <tr>
            <th>Click to use Fill Down:</th>
            <th><input type="button" name="copy" value="copy"></th>
            <th><input type="button" name="copy" value="copy"></th>
        </tr>
        <tr>
            <th>Student</th>
            <th>Test 1</th>
            <th>Test 2</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Student 1:</td>
            <td><input type="text" size="5" name="grade1"></td>
            <td><input type="text" size="5" name="grade1a"></td>
        </tr>
        <tr>
            <td>Student 2:</td>
            <td><input type="text" size="5" name="grade2"></td>
            <td><input type="text" size="5" name="grade2a"></td>
        </tr>
        <tr>
            <td>Student 3:</td>
            <td><input type="text" size="5" name="grade3"></td>
            <td><input type="text" size="5" name="grade3a"></td>
        </tr>
        <tr>
            <td>Student 4:</td>
            <td><input type="text" size="5" name="grade4"></td>
            <td><input type="text" size="5" name="grade4a"></td>
        </tr>
        <tr>
            <td>Student 5:</td>
            <td><input type="text" size="5" name="grade5"></td>
            <td><input type="text" size="5" name="grade5a"></td>
        </tr>
        <tr>
            <td>Student 6:</td>
            <td><input type="text" size="5" name="grade6"></td>
            <td><input type="text" size="5" name="grade6a"></td>
        </tr>
    </tbody>
    </table>
    </form>
    </body>
    </html>

  3. #3
    Join Date
    Oct 2010
    Posts
    45

    Genius!

    Thank you! A wonderfully simple example that works great. I really appreciate your help!

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