0

I'm trying to add an a tag to a table cell. My code is working all the way up to the final line shown here, at which point I get an exception of

'TypeError: Object doesn't support this property or method'

From what I've read, appendChild should be a method on my table cell.

    var rowId = "#rowid-" + response.id;
    var actionCell = $('#itemlist').find(rowId).find(actionCellId);
    //Add my Edit link                
    link = document.createElement('a');
    link.setAttribute('href', '/MYSite/Item/Edit?itemID=' + response.id);
    link.setAttribute('id', 'edit-' + response.id);
    var linkText = document.createTextNode('Edit');
    link.appendChild(linkText);
    //Exception occurs on this line
    actionCell.appendChild(link);
Jeff Reddy
  • 5,551
  • 9
  • 55
  • 88
  • 1
    Why arent you just using the jQuery append? – Mech Oct 12 '11 at 18:30
  • I'll have to look that up. I'm pretty new to JQuery and not the most experienced javascript developer either. – Jeff Reddy Oct 12 '11 at 18:32
  • `actionCell` is a jQuery object. It doesn't have the `appendChild()` method. I agree with @MechSoftware: it's better not to mix and match jQuery and pure JavaScript methods. – JJJ Oct 12 '11 at 18:33
  • I found what I needed to do by googling javascript as I couldn't find it googling jquery. That's the reason for the mix. Once @Mech Software suggested jQuery append, I found the solution. I didn't know where to search. – Jeff Reddy Oct 12 '11 at 22:09

1 Answers1

4

Replace this:

link = document.createElement('a');
link.setAttribute('href', '/MYSite/Item/Edit?itemID=' + response.id);
link.setAttribute('id', 'edit-' + response.id);
var linkText = document.createTextNode('Edit');
link.appendChild(linkText);
//Exception occurs on this line
actionCell.appendChild(link);

With this:

$('<a>Edit</a>').attr({
    'href': '/MYSite/Item/Edit?itemID=' + response.id,
    'id': 'edit-' + response.id
}).appendTo(actionCell);
wanovak
  • 6,117
  • 25
  • 32
  • Worked like a charm. I tried doing something similar looking at the append method, but my solution didn't work. Thanks a bunch. – Jeff Reddy Oct 12 '11 at 18:43