How co I call this function from within another function?

I have two buttons with event listeners:

save.addEventListener('click', save);
open.addEventListener('click', open);

The save button call this function:

var save = function() {
...
};

The open button calls this function:

var open = function() {
...
};

For useability reasons, I want the open function to be called right after the save button is finished. Using open(); at the end of the function doesn’t work. What’s the correct syntax for calling another function when it starts with var? (This is called an anonymous function, right?)

Since when is var open or var save valid?

That’s an example. Actually they are setSave and setOpen.

Why not?

You haven’t given us enough information to answer your question.

<button id="open">Open</button>
<button id="save">Save</button>

<script>

var open = function() {
  console.log('do open stuff')
}

var save = function() {
  console.log('do save stuff')
  
  open()
}

document.getElementById('open').addEventListener('click', open)
document.getElementById('save').addEventListener('click', save)

</script>

I see my mistake. xxxxx(); does indeed call a function that begins like var xxxxx = function() {

My example did not work because I was linking to a function that wasn’t a good successor, so nothing seemed to happen.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.