2

I am creating an ul and adding li to it with data in it using jquery append function. Inside this append function I am calling another function to parse more xml data and add it to the ul. But this function displays only [object Object]. Whereas when I console.log(li) inside func() function then it displays the correct li.

here is my code

container.append(
            '<ul>'
                + '<li>'
                    + '<h4 class="title stitle">' + from 
                        + '<section><span class="subtitle">' + routeTime + ' mins    ' + routeDist + ' km</span></section><br>'
                    + '</h4>'
                    + '<ul class="contents">'
                        + '<li>' + '<img src="' + walk + '" />' + '</li>'
                        + '<li>' + '<span class="">' + startTime + '   ' + distance + ' km</li>'
                        + func(lines) //<-- function call here
                    + '</ul>'
                + '</li>' +
            '</ul>'
            );

here is the func function code

func = function (lines) {
    var li = $('<li></li>');

    lines.each(function () {
        var stopel          = $(this),
            busCode         = stopel.attr('code').slice(1,4),
            stops           = stopel.find('STOP');

        li.append('<img src="' + bus + '" />')
        .append('<span>' + busCode + '</span>')
        .append('<section class="clear"></section>');

        stops.each(function() {
            var el2         = $(this).find('NAME'),
                deptime     = $(this).find('DEPARTURE');
            li.append('<p class="stop">' + deptime.attr('time') + '   ' + el2.attr('val') + '</p>');
        });
        li.append('<p class="last"></p>');
    });
    console.log(li);
    return li;
}

Where am I making mistake?

kapa
  • 77,694
  • 21
  • 158
  • 175
Om3ga
  • 30,465
  • 43
  • 141
  • 221

3 Answers3

1

The problem is that your function returns a jQuery object. And when you are using the + operator on this object, you are using it as a string. So an attempt is made to convert it to a string, with the result of [object Object].

The jQuery object has a method called .html() which returns the HTML content of that jQuery object as a string - which you can safely use in the concatenation. In your case though, you would need an .outerHtml() method, which does not exist in jQuery, but you could add it (see this question). Alternatively you could simply use the DOM (not jQuery) .outerHTML property, Firefox used to not support it, but they now do (from version 11, correct me if I'm wrong).

You can call the method on the return value of the func like this:

...
+ '<li>' + '<span class="">' + startTime + '   ' + distance + ' km</li>'
+ func(lines).outerHtml() //<-- function call here
+ '</ul>'
...

Or if you want the DOM way:

+ func(lines).get(0).outerHtml //<-- function call here

Quick suggestion: you should use a templating solution like Mustache (my preference), Handlebars, etc. Creating HTML by concatenating strings in Javascript is ugly, hard to maintain and violates the rule of separation of concerns. When you try one of these libraries, you will wonder how could you live without it.

Community
  • 1
  • 1
kapa
  • 77,694
  • 21
  • 158
  • 175
  • I did your way but now it does not concatenate the `li` but only the contents inside `li`. How can I make to so that the `li` including its content gets attached? – Om3ga Nov 17 '12 at 14:22
  • @x4f4r Hm, that's true. This question should help: http://stackoverflow.com/questions/2419749/get-selected-elements-outer-html – kapa Nov 17 '12 at 14:25
  • @x4f4r Updated my answer to address your problem. – kapa Nov 17 '12 at 14:31
  • yep that link helped. Thanks :) – Om3ga Nov 17 '12 at 14:32
1

When you do var li = $("<li></li>"), you are passing a reference to instance of an List Object created for the DOM. You are manipulating this object and hence you are getting the output as [object Object] whereas in the Console you are seeing the HTML for that Object.

You should stringify the li object reference or use it in $.html(li) or li.html();

Rahat Khanna
  • 4,570
  • 5
  • 20
  • 31
0

You're returning the jQuery object, but looks like you want the HTML.

Try this at the end of your function:

return li.outerHTML;
Christian
  • 19,605
  • 3
  • 54
  • 70