Solved

fill an empty table

Posted on 2014-02-13
6
293 Views
Last Modified: 2014-02-13
I'm trying to fill an empty table (well, actually remove everything then populate it) during an ajax call and can't seem to get it working. Here's the code I'm using:

$('#tblOtherUserResults > tbody:last').remove().append(results);

results is a valid markup of a table row, such as "<tr><td>1</td></tr>" and I've verified it is indeed valid. I've tried it without the reference to the tbody and nothing works.

any help is  greatly appreciated!
0
Comment
Question by:Big Monty
  • 2
  • 2
  • 2
6 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39857774
What about :
$('#tblOtherUserResults').html(results);

If it don't work, please provide a link to your page or setup one here :
http://jsfiddle.net
0
 
LVL 33

Author Comment

by:Big Monty
ID: 39857873
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39857886
Your function need to be accessible
Currently you have it in a document.ready/window.onload block

Work : http://jsfiddle.net/vL7CU/3/

Better, use jQUery to bind
Work : http://jsfiddle.net/vL7CU/4/

function insertStuff() {
    var results = '<tr><td>2</td></tr>';
    $('#tblOtherUserResults').html(results);
}

$(document).ready(function() {
    $("button").click(insertStuff);
});

Open in new window


<button>insert</button>
<br>
<table id="tblOtherUserResults">
    <tr>
        <td>1</td>
    </tr>
</table>

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 39857919
Couple things:

In the jsfiddle example you gave, you don't actually have a tbody element in there, so you're going to get a jquery result with 0 length and nothing will happen.

Though leakim's suggestion is perfectly valid, I would generally lean towards yours.  .html will kill any header rows if you have them as well, while calling the append will keep the headers in tact and just replace the body.

Main other problem with your sample, is you're also calling .remove() on the tbody, where you should be calling .empty().

.remove will remove the entire tbody element from the DOM, so you'll no longer have anything to .append() the results to.

Make sure you actually have the tbody element in there, and change the call to .empty() and it should work.
0
 
LVL 33

Author Closing Comment

by:Big Monty
ID: 39858054
the issue was using remove() over instead of empty(). Once I switched to that it worked with the append()

the fiddle was a very quick and dirty attempt to show what i was doing attempting to do in the most simplest terms. essentially i'm making a an ajax call whenever a textbox changes and displaying the results in the table.

either way everythings working now thx
0
 
LVL 22

Expert Comment

by:Snarf0001
ID: 39858113
Great, glad it's working.

Should note though, the only reason I suggested against leakim's, was because it was calling .html on the whole table instead of the tbody.
Calling .html on the full tbody selecter instead of just the table would have the same effect.
I'd be surprised if there was any difference in perf, since under the covers jquery's doing the same thing, but still one method call instead of two.

Food for thought.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question