Galaf
Galaf

Reputation: 57

JQuery, html() on a table cell returns "undefined is not a function"

I've been having some problems with JQuery.

I have a HTML table with the names having the class .ulName When I retrieve them with $('.ulName'), I have no problem. When I iterate over them with alert(), Chrome tells me they are indeed HTMLTableCellElement. But when I use the html() method on them, I get "Uncaught TypeError: undefined is not a function". I tried to use other methods to see how it would work (append() for example) but it did the same thing.

I also tried to change from a forloop to $.each but it did the same thing.

And finally, I also tried $.parseHTML but it returns null.

Here is the code source, I hope you can help me with my problem because I don't see why it wouldn't work. Thank you in advance for your answers.

$('#ulFilter').on('input', function () {
    var uploads = $('.ulName');

    for (var i=0; i < uploads.length; i++) {
        alert(uploads[i].html());
    }
});

Upvotes: 1

Views: 1777

Answers (2)

Stefano Dalpiaz
Stefano Dalpiaz

Reputation: 1673

Using the indexer gets the raw DOM element, not the jQuery object. See this question.

The raw element does not have a function called html, but instead a property called innerHTML.

Here are a few solutions:

  • using uploads.eq(i) instead of uploads[i]
  • using $(uploads[i]) instead of uploads[i]
  • using uploads[i].innerHTML instead of uploads[i].html()

Upvotes: 5

mohamedrias
mohamedrias

Reputation: 18576

$(".ulName") returns you a NodeList.

So you can use,

var uploads = $(".ulName");
$.each(uploads, function() {
    $(this).html();
});

Upvotes: 2

Related Questions