Preserve Javasript State of controls after postback asp.net

While working with jquery controls like dynatree, chosen etc, you might have faced problems like once have set init event to controls through jquery and then postback (even in update panel), the control is reset by asp.net .This can be preserved upon postback by using simple jquery of asp.net.
The trick is to add an event ‘endRequest()’ event and initialize the jquery control here.
Below is an example:

    <script type="text/javascript">
        $(function () {
            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function (sender, e) {
                //initialize your control here
                //example of chosen select
                $(".chosen-select").chosen({ max_selected_options: 5 });
            });
        });
    </script>