Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140042

How to get the value of attribute instead of an [Object object] ? (JavaScript)

$
0
0

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 !!!


Viewing all articles
Browse latest Browse all 140042

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>