Solved

fill an empty table

Posted on 2014-02-13
6
292 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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)
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…

911 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

18 Experts available now in Live!

Get 1:1 Help Now