0

I'm trying to add a new Table row with an input type file, after each time someone chooses a file to upload.

The problem is, the input type is not inserted into the new row;

The markup language:

<table id = "images">
    <tr>    
        <td>Title: </td><td><input type="text" name="title" size="51" required><td>
    <tr>
    <tr>
        <td>Description:  </td><td><textarea type="text" name="story" rows="10" cols="60"></textarea><td>
    <tr>
    <tr>
        <td>Author:  </td><td><input type="text" name="auth" size="51" required></textarea><td>
    <tr>
    <tr>
        <td>Image:  </td><td><input type="file" name="image[]" size="20" accept="image/jpeg, image/png" required onChange="addRow('images')"></input><td>
    <tr>
</table>

The JS:

<SCRIPT language="javascript">          
    addRow(tableID) 
    {       
        var table = document.getElementById(tableID);

        var rowCount = table.getElementsByTagName("tr").length;
        var row = table.insertRow(rowCount-2);

        var cell1 = row.insertCell(0);
        cell1.innerHTML = "Images";

        var cell3 = row.insertCell(1);
        var element2 = document.createElement("input");
        element2.type = "file";
        element2.name = "image[]";

        document.getElementsByName("image[]").lastChild.setAttribute("onChange", "addRow('images')");

        cell3.appendChild(

    }
</SCRIPT>

The error I'm seeing is:

Uncaught TypeError: Cannot call method 'setAttribute' of undefined

Any help greatly appreciated.

Peter

3 Answers 3

2
function addRow(tableID) 
    {

        var table = document.getElementById(tableID);

        var rowCount = table.getElementsByTagName("tr").length;
        var row = table.insertRow();

        var cell1 = row.insertCell(0);
        cell1.innerHTML = "Image:";

        var cell3 = row.insertCell(1);
        var element2 = document.createElement("input");
        element2.type = "file";
        element2.name = "image[]";

        cell3.appendChild(element2);
        element2.setAttribute("onChange", "addRow('images')");
    }

here's a working fiddle: http://jsfiddle.net/C3GRN/1/

Sign up to request clarification or add additional context in comments.

Comments

2

JAVASCRIPT

document.getElementById('images').innerHTML+="<tr><td>blah-2</td></tr>";

JQUERY

$('#images tr:last').after('<tr><td>blah-2</td></tr>');

Comments

1
    $('#images tr:first').after('<tr><td><input type="text" ></td></tr>');  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.