Reuse JS code from a bootstrap-datepicker

Asked

Viewed 27 times

0

I have a bootstrap-datepicker with the following JS:

    $(document).ready(function() 
        {
            var dataIni = $("#calendarioIni");
            var dataFim = $("#calendarioFim");

            $('#calendarioIni').datepicker({
                startDate: '01/01/1998', 
                endDate: '31/12/1998', 
                dateFormat: 'dd/mm/yy',

            });

            $('#calendarioFim').datepicker({
                startDate: '01/01/1998',
                endDate: '31/12/1998', 
                dateFormat: 'dd/mm/yy',

            });
 });

But I will use it on several pages in my project, how could I reuse code?

1 answer

1


If on all pages the element id is the same you can simply add this snippet of code to a . js and import it on all pages so when you want to make any change just modify the . js

It would look something like this :

<script src="seu-arquivo.js" type="application/javascript"></script>

Inside of your pages . html

  • it would be ideal if I create a . js just to put it?

  • Not necessarily. You can take advantage of this js to put codes that "repeat" on most pages in order to have a smoother maintenance.

  • good, for now only this datepicker that will repeat even...

  • the code would be exactly the same? including this part: $(Document). ready(Function()? I think it’s only Function that is right?

  • 1

    This ready() in Document means, after loading it, to my point of view it can continue like this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.