Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force a blur event to occur in JavaScript?

Here's what I want to do. I want to trigger an event every time a select element changes. I have a multiline select and when I make changes (click on elements), it does not change until the select box loses focus. So I'm trying to force a blur every time the select box is clicked. That way if it changes, it will trigger the changed event. If it doesn't change, nothing will happen.

How do I do this? Am I even approaching this the right way? Jquery answers are okay as well.

like image 218
Jason Thompson Avatar asked Sep 03 '10 15:09

Jason Thompson


People also ask

How do you blur something in JavaScript?

Methods focus/blurblur() set/unset the focus on the element. It works in all browsers except Firefox (bug). If we enter something into the input and then try to use Tab or click away from the <input> , then onblur returns the focus back.

What triggers Onblur?

The onblur event occurs when an object loses focus. The onblur event is most often used with form validation code (e.g. when the user leaves a form field). Tip: The onblur event is the opposite of the onfocus event.

What is blur () method?

blur() method removes keyboard focus from the current element.


1 Answers

In addition to Ender the full code could be something like this.

$('#mySelectBox').change(function() {
    $('#thingToBlur').blur();
})

Reference: http://api.jquery.com/blur/

like image 77
Muffun Avatar answered Nov 15 '22 19:11

Muffun