John Smith John Smith - 2 years ago 218
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:


Answer Source

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) {
          $('<span/>', {
              "class" : "input-group-addon", 
              text    : 'Auswählen'

which is more readable IMO.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download