JavaScript dynamically element duplicate each time function is called

Reddi

I have a function called validateForm() which checks for me form input. If the input is not as it should be (each field filled) alert message should appear and should be removed after 3 seconds and it does, but each time I call this function this element is created in DOM. How can I improve that code? What I want to get is div with alert message as below, but not created in the DOM each time the function is called, but once.

static validateForm() {
    const title = document.querySelector('#title').value;
    const author = document.querySelector('#author').value;
    const isbn = document.querySelector('#isbn').value;

    if (title === '' || author === '' || isbn === '') {
        const div = document.createElement('div');
        div.className = 'alert alert-dismissible alert-danger';
        const message = document.createTextNode('Please fill all fields before adding.');
        div.appendChild(message);

        const bookForm = document.querySelector('#book-form');
        bookForm.parentNode.insertBefore(div, bookForm);


        setTimeout(() => {
            div.classList.add('d-none');
        }, 3000);

        return false;
    }

    return true;
}
McBern

Try this:

//construct div globally
var div = document.createElement('div');
div.className = 'alert alert-dismissible alert-danger';
div.style.display = "none";
const bookForm = document.querySelector('#book-form');
bookForm.parentNode.insertBefore(div, bookForm);


static validateForm() {
    const title = document.querySelector('#title').value;
    const author = document.querySelector('#author').value;
    const isbn = document.querySelector('#isbn').value;

    if (title === '' || author === '' || isbn === '') {
        const message = document.createTextNode('Please fill all fields before adding.');
        div.html = message;
        div.style.display = "block";

        setTimeout(() => {
            div.classList.add('d-none');
        }, 3000);

        return false;
    }

    return true;
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Generate a sequential or random value each time a function is called

分類Dev

Are variables re-initialized each time a function in an external file is called in Python 2.7

分類Dev

How to make a member function of a class in C++ generate a different random number each time it is called?

分類Dev

Get element from which onclick function is called

分類Dev

Python decorator function called at compile time

分類Dev

Distributing nested lists one element at a time each

分類Dev

Not have to create new element each time?

分類Dev

Duplicate each element of a vector and add "a" to odd and "b" to even elements

分類Dev

Parent controller instantiated each time a child view is called

分類Dev

Creating a new thread each time a method is called using pthread

分類Dev

ASP.NET Function called in JavaScript

分類Dev

How to evaluate time of each function in a class

分類Dev

How do I dynamically focus on input element at run time

分類Dev

which value does javascript function takes if the function is called in the given example?

分類Dev

jQuery append html element but with a different class name each time

分類Dev

How to dynamically check if an element has mulitple class using jquery or javascript

分類Dev

Dynamically move a div above a list element using Javascript

分類Dev

firefox extension / javascript - how to access dynamically created element

分類Dev

Can I store a function rather than its result to a dictionary value so that it is dynamically updated when the value is called?

分類Dev

ddply/transform does not apply function to each element of a character vector

分類Dev

jQuery element.closest(...).attr is not a function when using each

分類Dev

How to use a scipy function on each element of a tensor using Keras?

分類Dev

how to set onchange function to each append div element

分類Dev

How to apply a function to each element of a list or vector in Clojure

分類Dev

Apply function to each element of multiple lists; return differently named dataframes

分類Dev

Function for getting one instance of each element in a vector in R

分類Dev

applying a Function to Each element in an array (O'Reilly cookbook example)

分類Dev

Javascript Check in the function, if it being called directly or from an object

分類Dev

Angular 4+ Function reloading each time the component loads

Related 関連記事

  1. 1

    Generate a sequential or random value each time a function is called

  2. 2

    Are variables re-initialized each time a function in an external file is called in Python 2.7

  3. 3

    How to make a member function of a class in C++ generate a different random number each time it is called?

  4. 4

    Get element from which onclick function is called

  5. 5

    Python decorator function called at compile time

  6. 6

    Distributing nested lists one element at a time each

  7. 7

    Not have to create new element each time?

  8. 8

    Duplicate each element of a vector and add "a" to odd and "b" to even elements

  9. 9

    Parent controller instantiated each time a child view is called

  10. 10

    Creating a new thread each time a method is called using pthread

  11. 11

    ASP.NET Function called in JavaScript

  12. 12

    How to evaluate time of each function in a class

  13. 13

    How do I dynamically focus on input element at run time

  14. 14

    which value does javascript function takes if the function is called in the given example?

  15. 15

    jQuery append html element but with a different class name each time

  16. 16

    How to dynamically check if an element has mulitple class using jquery or javascript

  17. 17

    Dynamically move a div above a list element using Javascript

  18. 18

    firefox extension / javascript - how to access dynamically created element

  19. 19

    Can I store a function rather than its result to a dictionary value so that it is dynamically updated when the value is called?

  20. 20

    ddply/transform does not apply function to each element of a character vector

  21. 21

    jQuery element.closest(...).attr is not a function when using each

  22. 22

    How to use a scipy function on each element of a tensor using Keras?

  23. 23

    how to set onchange function to each append div element

  24. 24

    How to apply a function to each element of a list or vector in Clojure

  25. 25

    Apply function to each element of multiple lists; return differently named dataframes

  26. 26

    Function for getting one instance of each element in a vector in R

  27. 27

    applying a Function to Each element in an array (O'Reilly cookbook example)

  28. 28

    Javascript Check in the function, if it being called directly or from an object

  29. 29

    Angular 4+ Function reloading each time the component loads

ホットタグ

アーカイブ