5

below is my code.

$("<table/>",{"cellspacing":"0","cellpadding":"0","border":"0","width":"100%"})
.append(
$("<tbody/>")
.append(function(){

    options=["ONE","TWO","THREE","FOUR"];
    $.each(options, function(val) {
        return ($("<tr/>")
        .append(
            $("<td/>").html("4/6/2013 10:41"),
            $("<td/>").html("5/6/2013 10:42"),
            $("<td/>").html(val),
            $("<td/>").html("<img src='pdf_16x16.png'/>"),
            $("<td/>").html("<a href='#'>Re-Upload Documents</a>")
        ));
    })

})
).appendTo("body");

for loop inside the append is not working.

3
  • val is the index here. Is it what you expect? ` $.each(options, function(i,val) {...});` ? Commented Aug 7, 2013 at 10:39
  • How does you html look like? Commented Aug 7, 2013 at 10:39
  • Typo: you have two closing brackets after the append, you need curly brace and a bracket: }); Commented Aug 7, 2013 at 10:40

4 Answers 4

5

The problem is because you are not returning anything from the append function, only the each loop within it. Try this:

$("<tbody/>").append(function(){
    options = ["ONE","TWO","THREE","FOUR"];
    var $container = $('<table></table>');

    $.each(options, function(val) {
        $container.append($("<tr/>").append(
            $("<td/>").html("4/6/2013 10:41"),
            $("<td/>").html("5/6/2013 10:42"),
            $("<td/>").html(val),
            $("<td/>").html("<img src='pdf_16x16.png'/>"),
            $("<td/>").html("<a href='#'>Re-Upload Documents</a>")
        ));
    });

    return $container.html();
});

Example fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. it is working. but it should be tr not div in container variable. great!.
@Natesan Why? He is returning the content not the element. Maybe he could use a TABLE but not a TR
1

Try

var tbody = $("<tbody/>").appendTo($("<table/>", {
    "cellspacing" : "0",
    "cellpadding" : "0",
    "border" : "0",
    "width" : "100%"
}).appendTo("body"));

options = ["ONE", "TWO", "THREE", "FOUR"];
$.each(options, function(key, val) {
    return tbody
    .append($("<tr/>")
            .append($("<td/>").html("4/6/2013 10:41"))
            .append($("<td/>").html("5/6/2013 10:42"))
            .append($("<td/>").html(val))
            .append($("<td/>").html("<img src='pdf_16x16.png'/>"))
            .append($("<td/>").html("<a href='#'>Re-Upload Documents</a>")));
})

Demo: Fiddle

Comments

0

Try like

$.each(options, function(val) {
    return ($("<tr/>")
    .append(
        $("<td/>").html("4/6/2013 10:41") + 
        $("<td/>").html("5/6/2013 10:42") +
        $("<td/>").html(val) +
        $("<td/>").html("<img src='pdf_16x16.png'/>") +
        $("<td/>").html("<a href='#'>Re-Upload Documents</a>")
    ));
})

Comments

0

try following:

$("<table/>",{"cellspacing":"0","cellpadding":"0","border":"0","width":"100%"})
.append("<tbody></tbody>").appendTo("body");

$("tbody").append(function(){
  options = ["ONE","TWO","THREE","FOUR"];
   var $container = $('<div></div>');

$.each(options, function(val) {
    $container.append($("<tr/>").append(
        $("<td/>").html("4/6/2013 10:41"),
        $("<td/>").html("5/6/2013 10:42"),
        $("<td/>").html(val),
        $("<td/>").html("<img src='pdf_16x16.png'/>"),
        $("<td/>").html("<a href='#'>Re-Upload Documents</a>")
    ));
});

  return $container.html();
});

working fiddle here: http://jsfiddle.net/mCLsm/2/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.