busoni34
busoni34

Reputation: 499

Debugging onFocus event using Chrome Developer Tools? Can't return focus after break point

I'm trying to debug a JavaScript onFocus event attached to a bunch of text boxes on a page. The bug occurs when selecting a text box and then tabbing to the next text box. I'm trying to debug this by placing a break point in the onFocus event using the Chrome Developer Tools. The problem I'm facing is that when I select a text box and the break point is caught, Chrome Developer Tools steals focus and does not return it, so I can't tab to the next text box. Anyone have an idea for a work around? I guess I can resort to alert statements to print all of the information I need without using Chrome Developer Tools......

Upvotes: 20

Views: 18490

Answers (4)

r3m0t
r3m0t

Reputation: 1870

There's a checkbox in the Rendering tool of Chrome DevTools labelled "Emulate a focused page". This prevents the webpage from getting blur events when you click on DevTools or other windows.

Upvotes: 1

aminimalanimal
aminimalanimal

Reputation: 449

Chrome Dev Tools includes a Play/Pause button both in the Inspector and as an overlay to the webpage. Using the overlay prevents focus from landing on the Inspector.

Also, I've found the following type of logging solution to be easier to track than the interval method (thanks to less redundancy and the ability to pick up on changes that occur more rapidly than the interval):

$('*').on('focus blur', function(event) {console.log(event.type + " to:"); console.log(document.activeElement);});

Upvotes: 17

Malcolm Dwyer
Malcolm Dwyer

Reputation: 1155

One option for debugging tricky cases is to set an interval to poll the focus in the console.

setInterval(function() {console.log($(':focus')); }, 1000);

Type this in the console (update it to include whatever details you are interested in), hit enter, and then keep the console where you can see it while you do stuff in your UI.

*MDN docs for setInerval()

Upvotes: 13

Alexander Pavlov
Alexander Pavlov

Reputation: 32296

You are right, Chrome DevTools receive focus and do not restore it when you switch back to the debugged page. Feel free to file a bug at http://new.crbug.com (make sure you start the summary with "DevTools: " so that the bug can be assigned to the appropriate team as quickly as possible.)

On a side note, console.log() is a slightly better alternative to alert() as it allows formatted output.

Upvotes: 10

Related Questions