D
D
Denis_81062021-06-08 13:27:34
JavaScript
Denis_8106, 2021-06-08 13:27:34

How to turn on / off the function with one button?

How to enable and disable the function with one button (on pure JS)?
There is such an entry, but it does not work ....

document.querySelector('#offer button').addEventListener('toggle', createContent);

function createContent() {
....
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Insolent Muzzle, 2021-06-08
@Denis_8106

The function cannot be turned on and off, it can only be called. Two functions can be used:

function createContent() { ... }
function deleteContent() { ... }

There is also an option with a variable:
const content = function () {
    var isCreated = false
    const this.change = function () {
        if(isCreated) {
            isCreated = false
            ...
        } else {
            isCreated = true
            ...
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question