John Smith John Smith - 6 months ago 12
Javascript Question

`find()` undefined is not a function

I have this little jquery function:

$.each($('#TableBody tr:gt(0)'), function(index, element){
element.find('td').last().html($('<span/>', {class: "input-group-addon", text: 'Auswählen'}));
});


Somehow i get this error in the first line:

Uncaught TypeError: undefined is not a function


I tried some things in the console to fix this problem! But somehow nothing worked!

Now i hope you can help me! JSFIDDLE: http://jsfiddle.net/3C98M/

Thanks

Answer

element is not a jQuery object, the second argument of the $.each function returns the native DOM node

$.each($('#TableBody tr:gt(0)'), function(index, element){
    $(element).find('td').last().html($('<span/>', {"class": "input-group-addon", text: 'Auswählen'}));
});

Also note that class is a reserved keyword in javascript and should be quoted.
It could also be written

$('#TableBody tr:gt(0)').each(function(index, element) {
      $(element).find('td').last().html(
          $('<span/>', {
              "class" : "input-group-addon", 
              text    : 'Auswählen'
          })
      );
});

which is more readable IMO.

Comments