I am making custom class wrappers for form input fields that internally contain a DOM Node and are augmented with extra functionality method.
My questions is if there is a similar method to .toString() for appending to the DOM as I would like to directly insert my objects to the DOM instead of calling additional methods
In other workds, here is an example of what I have:
function A () {
this.element = documenet.createElement('input');
// blah blah logic
this.toString = function () {
return '<input type="'+this.element.type+'" value="'+this.element.value+'" />';
}
// a similar method to this i'ld like
this.toString = function () {
return this.element;
}
}
so that i can use it as follows:
var a = new A();
// this works as it calls .toString(), but it is a hack and it is not pretty
document.body.innerHTML += a;
// this is what i'd want to be able to do:
document.body.appendChild(a);
// this is what **I AM REALLY TRYING TO AVOID:**
document.body.appendCHild(a.toElement());
You can’t simply inherit from the DOM Node as it is not a public class
I’ve tried looking at other questions but none seem to have the answer… any ideas would be highly welcomed
You can’t inherit from the native DOM constructors, but you can inherit your wrapper class from jQuery!
With that, you could just
$(document.body).append(new A)ornew A().appendTo(document.body)etc.