0

I am creating table with input boxes and have used the following code to create new row.

I want new row should have blank values inside textboxes. For it I have tried class property to change but its not working and always get value from first row.

function addRow(tableID) {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;

                            // limit the user from creating fields more than your limits
        var row = table.insertRow(rowCount);
        var colCount = table.rows[0].cells.length;
        for(var i=0; i<colCount; i++) {
            var newcell = row.insertCell(i);
            newcell.innerHTML = table.rows[1].cells[i].innerHTML;
              document.getElementsByClassName('small')[0].setAttribute("value", ""); 
    }
}
1
  • The code searches all elements of the document. You can also use getElementsByClassNamedocument on a specific element. Untested, but you could try newcell.getElementsByClassName( instead (although better yet would be to loop through all elements after the loop with row.getElementsByClassName ) Commented Aug 29, 2017 at 10:29

2 Answers 2

1

You are setting the value of your first row only like so: document.getElementsByClassName('small')[0].setAttribute("value", "");

You need to loop through them all and set their values.

document.getElementsByClassName('small'),forEach(item => item.setAttribute('value','');

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

Comments

0

The value property does exactly what you need and works correctly in all scriptable browsers.

var input = document.getElementById("your_input");
input.value = "blah";

In your case, you can try-

document.getElementsByClassName('small')[0].value = "";

1 Comment

same result for this

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.