With optimized js enabled, my javascript code not working
The reason maybe is your custom script loads first before the Jquery script.

Try this:

(function($) {
  $(function() { 
    //your script here
  });
})(jQuery);


or

Drupal.behaviors.somethingHere = function (context) {
  //your script here
}