简体   繁体   中英

JS multiple function with the same name

I use different javascript function. The problem is that they have the same name so they overwrite each other. I would like to put a different name but so far it didn't work.

Here one of the function and how I use it:

<script type="text/javascript">     
    $(function() {
        $('#datepicker').multiDatesPicker({
            altField: '#date',
            dateFormat: "yy-mm-dd",
        });
</script>

<div id="datepicker"></div>

The other function start like this too $(function() {} and use the same way <div id="date"></div>

I try to put $(function name() but it didn't work. Do you have any idea? Thanks

This is a call to $ (in your case, it's jQuery), with a single argument, which happens to be an anonymous function. It doesn't have a name, and having more than one occurrence of this pattern will not "overwrite" the previous:

$(function() {
    // ...
});

Passing a function to $ is just shorthand for $(document).ready() . If you need more than one, you should be able to simply combine them:

$(function() {
    // Initialise your date picker
    // Do some other stuff
});

that function is shorthand for $(document).ready() which is just a binding to the ready event of the document. all that means is you can have as many of them as you want without overwriting eachother.

see the jsfiddle

    $(function()
  {
      alert('func1');
  });
$(function()
  {
      alert('func2');
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM