How to check if a sorted jquery list is sorted

Basically, I just want to know how to check if a sorted list is empty or not. I have an ajax call after updating the list, but if there are no elements, I want to get around the call because it will be superfluous.

What will be the method of achieving this? Is there an easy way?

+4
source share
1 answer

Using jQuery, you can try to select sorts and check how many items are returned:

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

The code above assumes that each of the elements of your sort has a class sortable-class . All you really need is a unique selector that only your sorts will find. For example, if your sorts are children of an element, your selector might 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 is some documentation if you do such things: http://api.jquery.com/length

+11
source

Source: https://habr.com/ru/post/1380273/


All Articles