0

Seeing if anyone can help me figure out why my element isn't focusing.

I'm using RiotJS events and tags. But I don't think this problem has to do with Riot since I tried it with an element on the DOM and pure JS and it still doesn't work.

In the console, when I ask for document.activeElement it gives me <input tabindex="0" id="box" type="text" placeholder="Search"> but it's not focused.

What's weirder is that I made an onblur function that focused back on the element onblur and it works! But when I tried tying it to the click [so I can trigger it via app.search.box.click();], it doesn't work.

I've googled a LOT and have tried a LOT of the suggestions on how to do this with pure JS.

I'm on OSX Yosemite. Using latest Chrome, Firefox AND Safari. No luck on anything. Please advise.

Here's my search.tag code:

<search>
  <a href="javascript:void(0)" id="close" onclick={ close }><i class="fa-remove fa"></i></a>

  <div class="elements">

    <input tabindex="0" id="box" type="text" placeholder="Search things" onkeyup={ search } onclick={ focusBox } />

    <span id="loading" class="hide"><i class="fa fa-gear fa-spin"></i></span>

    <a id="searchButton" href="javascript:void(0)" name="search">
      <i class="fa fa-arrow-right"></i>
    </a>

  </div>


  <script>
    this.search = function(event) {
      app.search.trigger('search', event, box.value);
    }

    this.close = function(event) {
      app.search.trigger('close');
    }

    this.focusBox = function(event){
      var element = event.target || event.srcElement;
      window.setTimeout(function() {element.focus()}, 1000);
    }


  </script>

</search>

Here's the search.js code that triggers the open:

search.open = function(event) {
  search.container.classList.remove('hide');
  search.container.classList.remove('closed');
  search.box.focus();
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
damusix
  • 111
  • 1
  • 9
  • It seems to work fine here, unless i'm misunderstanding your goals: http://jsfiddle.net/60t8we4y/ clicking the input, then clicking out, results in the input regaining focus 1 second later. – Kevin B Dec 11 '15 at 22:15
  • do you want it focused straight away when the page loads ? – Billy Dec 11 '15 at 22:40
  • After a certain event EG: A modal is open, or an off-canvas nav opens. – damusix Dec 18 '15 at 03:50

0 Answers0