Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 272
lofihelsinki Avatar asked Mar 07 '18 12:03

lofihelsinki


2 Answers

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);
}
like image 108
Aliaksandr Pitkevich Avatar answered Oct 10 '22 02:10

Aliaksandr Pitkevich


You can indeed use AutoNumeric.getAutoNumericElement(domElement) as Aliaksandr mentioned to check if an element is returned, but the official way to check if a DOM element is already managed by AutoNumeric is this:

AutoNumeric.isManagedByAutoNumeric(domElementOrSelector);

So your code should be updated like so:

let anObject;
if (!AutoNumeric.isManagedByAutoNumeric(inputSel)) {
    // Also, no need to querySelector `inputSel` here:
    anObject = new AutoNumeric(inputSel, autoNumericSettings);
}
like image 36
Alex Avatar answered Oct 10 '22 03:10

Alex