Quantcast
Channel: User spraff - Stack Overflow
Viewing all articles
Browse latest Browse all 55

onclick not firing on td when outside element has focus

$
0
0

I have a <input> which performs a search in Javascript when onchange, onkeydown etc fires. This builds a <table> of results which is displayed below the <input> using position: absolute. This works.

The table contains clickable cells

let td = document.createElement ("td");tr.appendChild (td);td.appendChild (document.createTextNode (search_result.label));td.onclick = create_click_handler (search_result);

The issue is that if the <input> has focus then onclick only fires on the second click. So if I perform this sequence of actions:

  • type search text (results are built)
  • click a <td> (nothing happens)
  • click the <input> to regain focus (nothing happens)
  • click a <td> (nothing happens)
  • click the <input> to regain focus (nothing happens)
  • click a <td> (nothing happens)
  • click the <input> to regain focus (nothing happens)
  • click a <td> (nothing happens)
  • click a <td> (NOW the onclick handler is called!)

When td.onclick fires, it runs correctly. If I trap "Event Listener Breakpoints" for Mouse > click in Firefox's Debugger, this confirms that onclick does not happen at all for the first click. In other words, this seems to be because of something in the DOM rather than something wrong with the Javascript.

The behaviour is the same in Firefox and Chrome, so I assume it's "correct" behaviour according to some standard.

How can I ensure the first click on this td always triggers the event handler?


Viewing all articles
Browse latest Browse all 55

Trending Articles



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