GoodSea88
GoodSea88

Reputation: 185

My function says Uncaught TypeError: $(...).reverse is not a function

    <script>
    $(document).ready(function () {
        $("#upBtn").click(function () {
            $("input:checked").each(function () {
                moveUp(this);
            })
        })
        $("#downBtn").click(function () {
            $("input:checked").reverse().each(function () {
                moveDown(this);
            })
        })
    });

    function moveUp(v) {
        let temp = $(v).parent().parent();
        temp.prev().before(temp);
    }

    function moveDown(v) {
        let temp = $(v).parent().parent();
        temp.next().after(temp);
    }
</script>

The moveUp Methode works fine.

But when I use moveDown, Chrome console says

Uncaught TypeError: $(...).reverse is not a function.

Any assistance would be much appreciated. Thanks.

Upvotes: 0

Views: 2699

Answers (1)

vol7ron
vol7ron

Reputation: 42099

jQuery doesn't have a reverse() that operates on a jQuery object.

Perhaps you meant $('input:checked').get().reverse(), which translates the object into a base array and then reverses it. Of course, in order to call each() (a jQuery method) on it, you'd have to convert it back to a jQuery object:

$( $('input:checked').get().reverse() ).each(function(){…})

Though, another option would be not to use each() and use vanilla JavaScript:

$('input:checked').get().reverse().forEach(elem=>{
  moveDown(elem)
})

Upvotes: 5

Related Questions