I always get [Object object]
or are creating a conflict no matter how hard I try. (I tried a couple of other variants.) ;(
var $ = function(selector){
var x;
var obj = {
myLib(selector){
return x || document.querySelectorAll(selector);
},
cl(selector){
x = [x[0].closest(selector)];
return this;
},
attr(s,v){
s&&v ? x.forEach(y=>{y.setAttribute(s,v);})
: x = [x[0].getAttribute(s)]; x.value; // what is wrong here ?
return this;
},
};
x = obj.myLib(selector);
return obj;
};
var wtf = $('.kilo').cl('.uniform').attr('data-wrestler');
console.log('WTF: '+wtf);
console.log('WTF: '+JSON.stringify(wtf));
<div id="foxtrott">
foxtrott
<div class="uniform" data-wrestler="hulkster">
uniform
<div class="charlie">
charlie
<div class="kilo">
kilo
</div>
</div>
</div>
</div>
If you also have useful links to read about this topic this would be very nice.
Thanks in advance, guys !!!
You should return x
instead of this
in the attr
method
var $ = function(selector){
var x;
var obj = {
myLib(selector){
return x || document.querySelectorAll(selector);
},
cl(selector){
x = [x[0].closest(selector)];
return this;
},
attr(s,v){
s&&v ? x.forEach(y=>{y.setAttribute(s,v);})
: x = [x[0].getAttribute(s)];
return x;
},
};
x = obj.myLib(selector);
return obj;
};
var wtf = $('.kilo').cl('.uniform').attr('data-wrestler');
console.log('WTF: '+wtf);
console.log('WTF: '+JSON.stringify(wtf));
<div id="foxtrott">
foxtrott
<div class="uniform" data-wrestler="hulkster">
uniform
<div class="charlie">
charlie
<div class="kilo">
kilo
</div>
</div>
</div>
</div>