2017-08-29 7 views
0

入力ボックスでテーブルを作成していますが、次のコードを使用して新しい行を作成しました。クラス入力値をjavascriptで変更します

新しい行にはテキストボックス内の空白の値が必要です。私はクラスプロパティを変更しようとしましたが、動作していないので、常に最初の行から値を取得します。

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", ""); 
    } 
} 
+0

try-できるコードは 'document'のすべての要素を検索します。特定の要素に対して 'getElementsByClassNamedocument'を使うこともできます。あなたは 'newcell.getElementsByClassName(')を試してみることができます(より良いのは、 'row.getElementsByClassName'でループの後のすべての要素をループすることです) –

答えて

0

あなただけそうのようなあなたの最初の行の値を設定している: document.getElementsByClassName('small')[0].setAttribute("value", "");

あなたはそれらすべてをループする必要があるとその値を設定します。

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

+0

コードに入れた後に出力がありません –

0

値プロパティは、あなたが必要なものを正確に行い、すべてのスクリプト可能なブラウザで正しく動作します。

お使いの場合には
var input = document.getElementById("your_input"); 
input.value = "blah"; 

、あなたは

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

同じ結果 –

関連する問題