?
Solved

fill an empty table

Posted on 2014-02-13
6
Medium Priority
?
298 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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
LVL 23

Accepted Solution

by:
Snarf0001 earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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)
Suggested Courses

770 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