This is my first post on StackOverflow, so at beginning I'm sorry for my not full fluent English ;) But I try explain with what I have problem.
Here is live example
class S {
constructor(selectors) {
let self = this;
this.elements(selectors);
}
elements(selectors) {
this.selectors = selectors;
let result = document.querySelectorAll(this.selectors);
if( result.length == 1 ) {
result = result[0];
this.element = result;
} else {
this.elements = [].slice.call(result);
}
this.nodes = result;
return this.nodes;
}
parent() {
let self = this;
if( !!this.element ) {
this.nodes = this.element.parentNode;
} else {
this.elements.forEach = (item, key) => {
self.elements[key] = item.parentNode;
};
this.nodes = this.elements;
}
return this.nodes;
}
result(a) {
return this.nodes;
}
}
window.$ = (selectors) => {
let el = new S(selectors);
return el;
};
console.log('first ex: ', $('#el') )
console.log('second ex: ', $('#el').parent() )
<html>
<body>
<div id="el">test</div>
</body>
</html>
If you open browser console you will see something like this:
[Log] first ex: –
S {selectors: "#el", element: <div id="el">, nodes: <div id="el">, …}
[Log] second ex: –
<body>…</body>
The second example is OK. I just want to return a HTML node.
In the first example it should return only <div id="el />
.
Any suggestions?