Search code examples
javascripthtmlautonumeric.js

How to check if element has autoNumeric initialized


How to check if an HTML element already has autoNumeric initialized, so i won't initialize it again?

Initializing it twice results in all input numbers appearing twice.

if (// autonumeric not yet initialized) {
    var anObject = new AutoNumeric(document.querySelector(inputSel), autoNumericSettings);
}

Edit: The reason to check is that autoNumeric might be previously initialized elsewhere, in another script with different autoNumericSettings and this script needs to respect the earlier set settings.


Solution

  • probably, you can use getAutoNumericElement(domElement) to check if an HTML element already has autoNumeric initialized

    if (AutoNumeric.getAutoNumericElement(domElement) === null) {
        var anObject = new AutoNumeric(document.querySelector(inputSel), autoNumericSettings);
    }