views:

48

answers:

2

This code prints output as given below... code:

$('#calendar').append('<table><thead><tr>');
    var i;
    for(i=0;i<=6;i++)
        {
            $('#calendar').append('<td>'+week[i]+'</td>');
        }
    $('#calendar').append('</tr></thead>');
    $('#calendar').append('</table>');

output:

<section id="calendar"><table><thead><tr></tr></thead></table><td>Sunday</td><td>Monday</td><td>Tuesday</td><td>Wednesday</td><td>Thursday</td><td>Friday</td><td>Saturday</td></section>

But i expect something in this structure..

<table><thead><tr><td>...</td></tr></thead></table>
A: 

I think appending in jQuery appends tags, rather than just text. Did you try to examine the intermediate results? Your first line will already result in <table><thead><tr></tr></thead></table> being appended to the section.

You can build the entire string you want to append, and then append it in one call.

OregonGhost
I can do this using html alone.. the purpose of using jquery was to increase functionality and decrease coding lines. ;)
Sakti
I don't see how building the string would be more code than you tried out with append. jQuery saves a lot of code, but it can't have some helper method for every little thing you can already do with plain JavaScript. This one may be interesting for using append: http://www.learningjquery.com/2009/03/43439-reasons-to-use-append-correctly
OregonGhost
+1  A: 

jQuery appends elements , not the literal text...since it's immediately appended to the DOM the way you're currently doing it, you'll get some very funny behavior, since you're starting with unclosed tags (which it closes for you to prevent errors).

You just need to change your approach a bit, but doing it right also means even less code, like this:

var row = $('<table><thead><tr></tr></thead></table>').find('tr');
for(var i=0; i<=6; i++) {
    $('<td />', { text: week[i] }).appendTo(row);
}
row.end().appendTo('#calendar');​

You can view a working demo here, this creates the table using $(html), a document fragment, we then .find() the row and keep a handy reference to it, create the <td> elements one by one using $(html, props), and add them to the row using .appendTo(). After we've added them all, call .end() to change the chain to what it was before the .find() was called...in this case the entire <table>, then just append that table to the #calendar element :)

Nick Craver
Thank you nick. Am just a beginner and its was gr8 to have some help.
Sakti
@Sakti: Welcome :) Did that resolve your issue?
Nick Craver
Yes it worked perfectlybut when i tried to do something similar to that it dint work ..var daym;daym = days[m];var cal = $('<tbody><tr></tr></tbody>').find('tr');if(x>0){$('<td />',{  ,colspan:x}).appendTo(cal);}var cd=1;while(cd<=daym){if(x=6){x=0;$('<tr />').appendTo(cal);}$('<td />',{ text: j+1,id:j+1}).appendTo(cal);cd++;x++;if (x!=6) { var rd = 6-cd;$('<td />',{  ,colspan:rd}).appendTo(cal);}}its the code to print the calender..i managed to print 1 to 30 in a single tr tagbut i want each week in separate tr tag..pls look into this.. if u r free..
Sakti
@Sakti: You can do something like this: http://jsfiddle.net/62yzW/ But there are several calendar things out there you can use already, e.g. http://jqueryui.com/demos/datepicker/#inline :)
Nick Craver
;) . am trying something like event calender .. not a datepicker .(:
Sakti