How to select current tag as DOM element in riotjs

210 views Asked by At

Is there any way to select current tag as DOM element without using id attribute?

What I'm currently doing is;

function addAttr(n,v){
  var ta = document.getElementById(opts.id);
  ta.setAttribute(n,v);
}

I'm looking for some direct way like this;

function addAttr(n,v){
  var ta = document.querySelector(this);
  //var ta = document.querySelector(this.root);
  ta.setAttribute(n,v);
}

Passing this.root works with jQuery but not with document.querySelector()

Complete example

1

There are 1 answers

0
Amit Kumar Gupta On BEST ANSWER

I got the solution. I moved my code in 'mount' event. So this.root is available. Now I don't need query selector. this.root works in itself.

Now I can directly use;

this.root.setAttribute(n,v);

I have updated plunker link for reference.

this.on("mount",function(){
  if(opts.fa){
    this.root.className += " faa";
    if(opts.selectable)
      this.root.innerHTML = fa_icons[opts.fa];
    else{
      this.root.setAttribute("faicon", fa_icons[opts.fa]);
    }
  }else if(opts.text){
    this.root.innerHTML = opts.text;
  }

  if(opts.transform){
    this.root.style.transform = opts.transform;
  }
});