Skip to content

Allow init from DOM element, JQuery object, or selector #92

@GregJohnStewart

Description

@GregJohnStewart

I've noticed that the initialization methods (new, init, initFromData) only support a string target identifier, i.e, "#input". This feels rather restrictive to me, as it requires me to know / have that selector string, as well as that element existing on the page DOM. When dealing with dynamic forms, this isn't always the case.

It would be great to make this more flexible, where I could pass in a "real" DOM element, or even a JQuery object, I.e,

let myInputDOM = document.getElementById("#input");
OverType.initFromData(myInputDOM, { /* defaults */ })

//bonus points for handling array of DOM elements:
let myInputDOMs = [];
myInputDOMs.push(document.getElementById("#input"));
OverType.initFromData(myInputDOMs, { /* defaults */ })

or

let myInputJq = $("#input");
OverType.initFromData(myInputJq, { /* defaults */ })

I don't think this would add much bloat, as likely in the initializers you could wrap the value in a normalizer into the resulting form the lib needs, in any case. I imagine you are likely already turning the selector into a DOM object(s) anyways, so essentially just adding a check?

I also think that the lib could handle Jquery objects without requiring the Jquery lib, as you can test the incoming object's fields and not need to interact with any direct JQuery bindings in order to get the plain js DOM objects (meaning JQuery need not be a dependency of this project to handle);

//example determining if object is jquery
let myInputJq = $(".input");
if (myInputJq.jquery) {
  console.log('jQueryObj is a jQuery object, version: ' + jQueryObj.jquery);
}

//getting/utilizing js DOM objs from Jquery
let firstElementJs = myInputJq[0]; //returns plain JS DOM element of first selected element

//foreach will iterate on JS DOM objects, not jquery ones
myInputJq.each(function(index, element) {
    //`element` is a js DOM element
});

This would make for much more flexible initialization; in a few cases I am dynamically creating input elements, and thus don't always have a direct / easy target selector string to utilize. It would be amazing to just init my element directly before I even add it to the DOM.

This topic also would apply to OverType.getInstance(element) as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions