views:

79

answers:

2

Hi All,

I am creating <td> and adding data to it.

$("<td>").addClass("tableCell1").text(mydata).appendTo(trow);

now if I have to create an <a> tag inside how to do it? example:

<td headers="xx">
                  <a href="#" title="zz</a>
                </td>

EDIT:

$("<td>").addClass("tableCell1").text(mydata).appendTo(trow); 

is working fine but

$('a').attr({ href: '#', title: 'title here' }).appendTo($('td')).appendTo(trow); 

is not working, I am not getting the <a> under <td>

+2  A: 

I would do something like this:

$("td.tableCell1").append('<a href="#" title="zz"></a>');

EDIT: Or for even more safety in selecting the proper <td>, you should be able to use

$("td.tableCell1", trow).append('<a href="#" title="zz"></a>');

For even EVEN more safety, you could

$("td.tableCell1:lastChild", trow).append('<a href="#" title="zz"></a>');
DLH
+1  A: 

I am assuming that you're trying to create the <a> and the <td> at the same time. If you're trying to add the <a> at a different event, you would need a different solution.

$("<td/>",{"class":"tableCell1", text:mydata})
    .append($("<a/>",{ href:"#", title:"zz"}))
    .appendTo(trow);

Note that you didn't indicate any content for the <a> in your question. If not styled properly, it will be invisible. If you want to add some text, add text: "some text" to the <a> creation.

patrick dw
@Sarfraz - You deleted your answer, but if you're watching, you need to pass an entire tag in order to *create* an element. `$('a')` selects, `$('<a>')` creates.
patrick dw