Good time of the day, forum users.

In html there is a table with two columns, I wrote js, to add a row to the table:

Js:

function func_table_without_forma() { var table = document.getElementById('tabl_one_without_forma'); var tr = document.createElement('tr'); tr.innerHTML = '<td><input name="edit_in_1" type="text" size="40"></td>\n\ <td><input name="edit_in_2" type="text" size="40"></td>'; var td = document.createElement('td'); tr.appendChild(td); table.appendChild(tr); }; 

Everything is working...
Then, I put a table (in html) in the form:

Js:

 function func_table_with_forma() { var table = document.forma7.tabl_one_with_forma; var tr = document.createElement('tr'); tr.innerHTML = '<td><input name="edit_in_1" type="text" size="40"></td>\n\ <td><input name="edit_in_2" type="text" size="40"></td>'; var td = document.createElement('td'); tr.appendChild(td); table.appendChild(tr); }; 

and somewhere an error occurs here.

Where did I make a mistake?
Thanks for the answer...

PS
html:

 <form name="forma7"> <table id="tabl_one_with_forma" width="100%" border="1" cellpadding="4"> <tbody> <tr> <td width="20%">111</td> <!-- height="59" --> <td width="50%">22772</td> </tr> </tbody> </table> <br><br> <button onclick="func_table_with_forma()"><b>with</b>_forma</button> </form> 

    2 answers 2

     function func_table_with_forma() { //var table = document.forma7.tabl_one_with_forma; var table = document.getElementById('tabl_one_with_forma'); var tr = document.createElement('tr'); tr.innerHTML = '<td><input name="edit_in_1" type="text" size="40"></td>\n\ <td><input name="edit_in_2" type="text" size="40"></td>'; var td = document.createElement('td'); tr.appendChild(td); table.appendChild(tr); return false; // что-бы не форма не отсылалась - возвращаем false }; 

    and in html:

     button onclick="return func_table_with_forma();"><b>with</b>_forma</button> 
    • I. @boris_U, I apologize (I recently sat down for JS), but how do I get the text of the error (I use NetBeans for writing)? Ii. And if there are several forms in html in which the tables have the same identifier, (what about "// var table = document.forma7.tabl_one_with_forma;")? - Konstantin78
    • one
      @ Konstantin78, an identifier with a specific name on the page can be only one, if this is violated, then the scripts will start to work unstably :) - MasterAlex
    • one
      @ Konstantin78; In the first question, try{}catch(){} ; in the second, the ID must be unique in the DOM space. Otherwise, no one can predict the behavior of any function that operates with DOM. - user31688
    • Yes, about uniqueness, I know, but I thought if the ID is in the form, then I can in the record "document.forma7.tabl_one_with_forma" be based on the form. On the first question, @TheDoctor, inserting cells - I so want to try inserting a record into a table, and then saving it to the database (so as not to do an additional handler ("control", "view" in kohana), because you need to insert Two values. And the option of adding through "element.innerHTML" found on one of the forums. - Konstantin78

    It would be just wonderful if you put the text of the error here.

    This code went berserk and hung jsFiddle.

    1) Why insert cells through element.innerHTML , and then create an empty cell and add it?

    2) The name='' properties are accessible through properties, but not id='' , so it finds the form, but the id inside does not. This is where everything flies.

    PS return false not necessary, use event.preventDefault() [ difference ].

    • jsfiddle.net/g6Ljdwjr in support of the @TheDoctor option with preventDefault () added 1 line to the author’s version - MasterAlex
    • cool! now the form is not sent due to: Error: ReferenceError: event is not defined then you need to call this function with the parameter event onclick = "func_table_with_forma (event);" and define it itself as: func_table_with_forma (e) {/// e.preventDefault (); } - Boris
    • event is a system object, it is always there. My example works (which is logical), on jsFiddle too. You have something wrong. - user31688
    • judging by the "explanation", it's okay, except for the loss of Ponte, in the use of "return false" - no. - Boris
    • @boris_U, scary - no, right - yes. The ascent is a necessary thing, it is unnecessary to cancel it, because we do not know what else the author wants to hang inside? - user31688