Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve "TypeError: $(...).length is not a function"? [duplicate]

My code is like this :

$(document).ready(function () {
    size_li = $("#myList li").length();
    x=3;
    $('#myList li:lt('+x+')').show();
    $('#loadMore').click(function () {
        x= (x+5 <= size_li) ? x+5 : size_li;
        $('#myList li:lt('+x+')').show();
    });
    $('#showLess').click(function () {
        x=(x-5<0) ? 3 : x-5;
        $('#myList li').not(':lt('+x+')').hide();
    });
});

Or see demo and full code here : http://jsfiddle.net/oscar11/6FzSb/4177/

I use jquery 3.0.1

When executed, there exist error :

TypeError: $(...).length is not a function

How can I solve it?

like image 341
moses toh Avatar asked Mar 13 '17 14:03

moses toh


3 Answers

Instead of this

$("#myList li").length();

Use this:

$("#myList li").length;

The $("#myList li") returns an array like object. All array like objects have a property called length, which when be read returns the number of items contained in the array like object. That being said there isn't any function called length. Hence length() is meaningless.

like image 137
Christos Avatar answered Oct 15 '22 07:10

Christos


length is a property, not a function, so remove the ()

$("#myList li").length;
like image 32
LinkinTED Avatar answered Oct 15 '22 05:10

LinkinTED


length is not a function it is just Yourthings.length without (). more doc at length documentation

like image 23
JustARandomProgrammer Avatar answered Oct 15 '22 06:10

JustARandomProgrammer