Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`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

like image 389
John Smith Avatar asked Dec 03 '22 19:12

John Smith


1 Answers

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.

like image 151
adeneo Avatar answered Dec 29 '22 02:12

adeneo