Search code examples
javascriptfirefoxfirefox-addonuser-inputdom-events

Firefox addons and extensions: getting user input


The setup: Say you click somewhere on this page, and you've got a Firefox extension that responds by inserting a <div> with an <input type="text"> into the document, positioned at the spot where you clicked.

The problem: I can't seem to get this text field to accept user input. It almost seems to be read-only. Is there some sort of configuration I have to set in chrome.manifest or elsewhere? Is getting user input this way allowed in Firefox extensions? If so, could someone please point me in the right direction?

Note: I've been able to set the value by using prompt(); but I'd rather just type in the <input> like normal.


Solution

  • Are you using event.preventDefault() to trap mouse clicks? That could be the reason why the text box seems read only – it can never gain focus. To fix this, use:

    textbox.addEventListener('click', function(event) {
        event.stopPropagation();
    }, false);
    

    This would keep the click event from bubbling up to your general event handler used to intercept most mouse clicks.