John Smith
John Smith

Reputation: 6259

`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

Upvotes: 4

Views: 12991

Answers (4)

Mdlc
Mdlc

Reputation: 7288

This is most likely not the answer to question of John Smith (very generic name by the way) but it hopefully will be relevant to users looking for the solution to the 'X is not a function' javascript error (for which this post is the top result).

It can be, that if you are loading multiple Javascript libraries, $ (shortcut for jQuery) get's overridden.

You could use:

 jQuery(element).find('td:last')....

Instead of

 $(element).find('td:last')....

To solve this (or remove the library that overrides jQuery).

Source: http://codeimpossible.com/2010/01/13/solving-document-ready-is-not-a-function-and-other-problems/

Upvotes: 0

ninhjs.dev
ninhjs.dev

Reputation: 8553

Wrap element with $() like the adeneo's answer or simply using this:

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

Upvotes: 1

theHacker
theHacker

Reputation: 4043

Surround element with $(...) to work

$(element).find('td:last')....

Upvotes: 3

adeneo
adeneo

Reputation: 318182

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.

Upvotes: 13

Related Questions