0

I'm not very good in jquery. I will call a jquery function from another jquery function, but it won't work.

<script type="text/javascript">
  $(document).ready(function () {
      
    $(".publish").click(function(){
        publish=true;
        $.fn.saveData();
    });
        

    $.fn.saveData = function() {
      \\ save something in database
    }
  }
</script>

The publish function will be called by a button, that works well. From this function I will call the saveData function, and that fails. Something is wrong, can someone help me?

Nico van Wijk
  • 241
  • 1
  • 9

1 Answers1

0

Since you try to define saveData in the same scope, and since it doesn't look like the current jQuery collection matters, there's no need for a custom jQuery method - just call a standard function:

const saveData = () => {
  // save something in database
}
$(".publish").click(function() {
  publish = true;
  saveData();
});

Live demo:

let publish = false;
const saveData = () => {
  // save something in database
  console.log('saving');
}
$(".publish").click(function() {
  publish = true;
  saveData();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="publish">click</button>
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • I try this but it will not work, saveData will not be called – Nico van Wijk Aug 29 '20 at 16:00
  • 1
    Works just fine here, see snippet – CertainPerformance Aug 29 '20 at 16:02
  • @NicovanWijk Why not? – Dave Newton Aug 29 '20 at 16:03
  • Sorry, it works!!! But what is this: const saveData = () => Is this a function? – Nico van Wijk Aug 29 '20 at 16:14
  • @NicovanWijk That is a function. – CertainPerformance Aug 29 '20 at 16:15
  • But it looks like an constante – Nico van Wijk Aug 29 '20 at 16:18
  • @NicovanWijk Yes, it's preferable to declare variables with `const` (IMO), because that makes it clear that they won't be reassigned. (Functions are variables too.) – CertainPerformance Aug 29 '20 at 16:18
  • Butt normaly in javascript you declare a function as Function dataSave() {} – Nico van Wijk Aug 29 '20 at 16:19
  • @NicovanWijk I (and many others) prefer using `const` because it makes it clear that the variable cannot be reassigned (and because arrow functions can be more concise in some situations, and because function declarations are hoisted, which can be counter-intuitive). You can use the `function` keyword instead, if you want, both work – CertainPerformance Aug 29 '20 at 16:21
  • @NicovanWijk "*normaly*" - not really. "Normally" (ie frequently) people namespace their functions, so it would be something like `var nico = {}; nico.saveDate = function() { ... }; nico.saveDate();` But that's overkill for an SO sample/demonstration, just that there are numerous different ways to generate a named function – freedomn-m Aug 29 '20 at 16:29
  • Bit of a long read, but worth knowing the difference: https://stackoverflow.com/questions/34361379/are-arrow-functions-and-functions-equivalent-exchangeable (for fyi, not meant to answer the question) – freedomn-m Aug 29 '20 at 16:33
  • @NicovanWijk When an answer solves your problem, you may consider upvoting and/or marking it as Accepted (check the checkbox on the left) to indicate that the issue is resolved :) – CertainPerformance Aug 29 '20 at 20:34