Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.trigger('blur') on collection of inputs?

Tags:

jquery

im trying to trigger('blur'); on a collection of inputs but i does not seem to get this right.

example is avaible here http://jsfiddle.net/VUUme/1/

im getting the collection and i got the blur method done but im not sure about the trigger part tho.

var $inputs = $('#form').find('input');

alert('load');

$inputs.each(function(){
$(this).trigger('blur');
});

//i tried this to but with no success
//$inputs.trigger('blur');

alert('after the blur');

$inputs.blur(function(){
    var $this = $(this);
    if ($this.val() == ''){
    alert('it works');
    }
});
like image 926
Dejan.S Avatar asked Nov 18 '11 11:11

Dejan.S


People also ask

What does trigger Blur do?

trigger( "blur" ) in the third. The blur event is sent to an element when it loses focus. Originally, this event was only applicable to form elements, such as <input> . In recent browsers, the domain of the event has been extended to include all element types.

What is input blur?

Input.blur( ) — remove keyboard focus from a form element.

What is the use of the blur function Java?

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

How can blurred events be prevented?

Preventing the blur If you want to prevent the blur event from being fired, you have to do so when you are inside the mousedown event, you can do so by invoking the method preventDefault() on the event. Click the checkbox, focus input & then click the button, the textfield never loses focus now.


1 Answers

Put trigger() after you define $inputs.blur():

alert('after the blur');

$inputs.blur(function(){
    var $this = $(this);
    if ($this.val() == ''){
    alert('it works');
    }
});

$inputs.trigger('blur');

Updated fiddle: http://jsfiddle.net/VUUme/3/

like image 65
Silver Light Avatar answered Oct 19 '22 13:10

Silver Light