'JQuery Swap 2 div's z-index

I tried to swap the z-index of two div elements.

The following code is my attempt to switch the two z-indices:

else if (pen_state == 1) {
                var pen_zindex = $("#pen").css("z-index");
                var curtain_zindex = $("#curtain").css("z-index");
                if (curtain_zindex < pen_zindex) {
                    $("#curtain").css('z-index',pen_zindex);
                    $("#pen").css('z-index',curtain_zindex);
                }
            }

But whatever I do, I can not swap them.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source