Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a jquery sortable list is empty

Tags:

jquery

Basically, I just want to know how to check if a sortable list is empty or not. I have an ajax call after a list is updated, but if there are no items I want to bypass the call because it will be unnecessary.

What would be the method to achieve this? Is there a simple method?

like image 484
Cyril Silverman Avatar asked Dec 22 '22 06:12

Cyril Silverman


1 Answers

Using jQuery you can attempt to select the sortables and test how many elements are returned:

if ($('.sortable-class').length > 0) {/*something with the class `sortable-class` exists*/}

The above code assumes that each of the elements in your sortable have the class sortable-class. All you really need is a unique selector that will only find your sortables. For instance if your sortables are all children of an element then your selector could look like this:

HTML

<ul id="sortable_parent">
    <li>this is sortable</li>
    <li>this is also sortable</li>
</ul>

JS

if ($('#sortable_parent').children('li').length > 0) {/*atleast one li exists*/}

Here's some documentation if you're into that kind of thing: http://api.jquery.com/length

like image 85
Jasper Avatar answered Dec 23 '22 21:12

Jasper