You can try something simple:
var elements = $('selector').click(function(){ $(this).css('z-index', max++); }), max = Math.max.apply(Math, elements.map(function() { return parseInt($(this).css('z-index'), 10) || 0; }).get());
This will cause each clicked item in the set to receive a z-index one higher than the current one.
Edit: A new method for calculating the maximum z-index using Math.max kindly provided by AndyE in the chat: http://chat.stackoverflow.com/transcript/message/96656#96656 p>
source share