Solved

fill an empty table

Posted on 2014-02-13
6
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 23

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 23

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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)

740 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