8

I'm completely new to JavaScript and jQuery and now I am trying to modify a html table.

Every time a button is clicked, the some stuff (input, text, button) should be added as the last row. I managed that for the first row, but when I click the button again, the next input will be added next to the last row and not under it. I don't understand why. Hope you can help.

This is my html file:

...
<main>
  <article id="main">
    ...
    <section id="neue vorschlaege">
      ...
      <table id="vorschlaege"> 
        <tr> 
          <td>Deine Idee</td>
          <td>Vorschläge</td>
          <td>Kategorie</td>
          <td>Löschen</td>
        </tr>
        <!-- I want to insert new rows here -->
      </table>
    </section>
  </article>
  ...
</main>

And this is my jQuery.js:

$(document).ready(function(){
  $('#abschicken').on('click', function(add){
    add.preventDefault();
    var name = $("#name").val();
    var fvv = $('input[name=FVV]:radio:checked').next('label:first').html();
    var zutaten = $("#zutaten").val();
    var passwort = $("#pw").val();

    $('#vorschlaege > tbody:last-child').append(
      '</tr>'
      +'<td><input type="checkbox" checked="true"></td>'
      +'<td>'+name+'</td>'
      +'<td>'+fvv+'</td>'
      +'<td><button id="loeschen">löschen</button></td>'
      +'</tr>');
  });
});
2
  • 2
    You are closing a tr in the first line of the append Commented Nov 23, 2016 at 13:03
  • The simplest solution.. Commented Nov 23, 2016 at 13:06

1 Answer 1

11

You started with a </tr>closing tag.

$('#vorschlaege > tbody:last-child').append(
            '<tr>'// need to change closing tag to an opening `<tr>` tag.
            +'<td><input type="checkbox" checked="true"></td>'
            +'<td>'+name+'</td>'
            +'<td>'+fvv+'</td>'
            +'<td><button id="loeschen">löschen</button></td>'
            +'</tr>');
Sign up to request clarification or add additional context in comments.

1 Comment

someone already commented on that... thank you. i can't believe i didn't see that. ;D

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.