Tom
Tom

Reputation: 12998

remove focus on select element on change and also if not changed with jquery

I have the following code which removes the focus of a select menu when the selected item is changed:

$("select").change( function() {
    $(this).blur();
});

However, if I do not select a different option to the one that was already selected, the blur() function does not run... because nothing has changed.

Is there a "change or not changed" function??

Upvotes: 4

Views: 7214

Answers (1)

Novak
Novak

Reputation: 2768

Try

var c = 0;
$("select").bind('click', function() {
    event.stopPropagation();
    if (c++ % 2 == 1)
    {
        console.log(c);
        $(this).blur();
    }
});

$('html').click(function() {
    if ($('select').is(':focus'))
        c = 1;
    else
        c = 0;
});
​

jsfiddle.net/5eE8x

Updated.

Upvotes: 1

Related Questions