0

I created a html page with a search form and I want to show a table with some data whenever the user clicks on the Search button.

I want to use JQuery to show such data instead than inserting a static html <tr>. I want to exploit the .append() method to add a string containing the <td> with data.

So I did the following:

I copypasted a bootstrap code for table and emptied the <tbody>, so the table won't show any row if results will not be returned.

I gave an ID to the <tbody>

This is the html code:

<table id="tabella" class="table hide">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">Nome</th>
      <th scope="col">Cognome</th>
      <th scope="col">Indirizzo</th>
    </tr>
  </thead>
  <tbody id="tbody">
<!-- 

    <tr>
      <th scope="row">1</th>
      <td>Maria</td>
      <td>Ottone</td>
    </tr>


    <tr>
      <th scope="row">2</th>
      <td>Giacomo</td>
      <td>Troisi</td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td>Lorenza</td>
      <td>Pieri</td>
    </tr>

 <-->

  </tbody>
</table>

What I need to do now is appending the first <tr> of the table (previuìously deleted) to the <tbody> by using the JQuery .append() method, as if it were a string, by selecting the ID of the <tbody>.

function validateNome(){
  console.log('function validateNome has been activated');

  if ($("#inlineFormInputNome").val()=="") {
      $("#errorLog").show();    
  } else {          
    $("#errorLog").hide();    
    $("#cercaNome").prop("disabled", true);     
    setTimeout(function (){              
      $("#tabella").show();    

//What should I write inside the parentheses in the following line?

      $("#tbody").append("        ");

       $("#cercaNome").prop("disabled", false);    
    } , 2000);
  }
}

That's to say: is it possible to combine the append() method with the html() method in JQuery? Or: How may I use JQuery .append() method to add a string containing html code?

3
  • 1
    Just FYI, you'll get more accurate answers more quickly if you show us the actual code instead of describing it Commented May 22, 2019 at 13:37
  • You can check this post if it helps in anyway: stackoverflow.com/questions/3015335/jquery-html-vs-append Commented May 22, 2019 at 13:39
  • @addo.lou thank you for suggesting it. Let's say that I'm trying to "fill that html() method inside the append() method" Commented May 22, 2019 at 13:49

1 Answer 1

1

You can try something like this:

 $("#tabella tbody").append('<tr><th scope="row">2</th><td>Giacomo</td><td>Troisi</td>/tr>');

It will appened the <tr> after the last <tr> that's in the table.

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

2 Comments

thank you! I tried to do that before but I left the spaces into the code; now, by looking at your snippet, I deleted the spaces between the tags and it works!
You're welcome! I also leave you this other post, might come in handy some other time: stackoverflow.com/questions/36974058/…

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.