Solved

fill an empty table

Posted on 2014-02-13
6
291 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 32

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 32

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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 invoke it on a web page. 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.: (CODE)

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now