Hi preena,you have to create clientLib and create your js file and put your code inside $(function(){//your js code here});and include that clientLib inside your component or page.How to create ClientLib :AEM Client Libraries explained by example Thanks