Paul Allen
Paul Allen

Reputation: 3

Input box losing focus due to broad targeting

I have an input box that I'm editing inline using jEditable (jQuery), but due to unfortunate circumstances it wasn't losing focus when I clicked outside of the input box, therefore I implemented the code below to combat that.

$('section').mousedown(function() {
    document.activeElement.blur();
});

This works fine for the problem I was having (not losing focus when clicking outside of the input box), but when it comes to clicking on my input box, it focuses fine, then loses focuses instantly, therefore it doesn't allow me to select any text in the input box. Clearly this is down to my broad targeting with 'section'. Is there a way to just use document.activeElement.blur(); without it targeting input elements?

Thank you.

Reference as to why I can't just fix the initial problem, seems it's implemented within the jQuery UI of sortable: Click on jQuery Sortable list does not blur input

Upvotes: 0

Views: 86

Answers (1)

user676853
user676853

Reputation:

I've come across this problem before using sortable, the best way to fix it is as follows:

$(document).click(function(evt) {
    var class_to_choose = $(evt.target).context.tagName.toLowerCase();

    if (class_to_choose!="input") {
        document.activeElement.blur();
    }
});

Basically just make sure that what you're clicking on isn't an input box.

Upvotes: 1

Related Questions