Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery onblur not working

Tags:

jquery

onblur

$('#lPass').focus(function() {  
        if (this.value == this.defaultValue) this.value = '';
        $(this).after('<input type="password" id="lPass" size="10" value="'+this.value+'"/>').remove();
}).blur(function() {
    alert(1);
});

<input id="lPass" type="text" size="10" value="Password"/>


onblur not working.
Any ideas?

like image 908
Isis Avatar asked Mar 01 '10 12:03

Isis


People also ask

What is Onblur jQuery?

jQuery blur() MethodThe blur event occurs when an element loses focus. The blur() method triggers the blur event, or attaches a function to run when a blur event occurs. Tip: This method is often used together with the focus() method.

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.

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 blur in angular?

A blur event fires when an element has lost focus. Note: As the blur event is executed synchronously also during DOM manipulations (e.g. removing a focussed input), AngularJS executes the expression using scope. $evalAsync if the event is fired during an $apply to ensure a consistent state.


1 Answers

Use live instead of focus and blur

.live() has been deprecated. Use .on() and .off() instead.

because you are adding the input in run time, so it adds to the page with out the events, live:

Attach a handler to the event for all elements which match the current selector, now or in the future.

Example:

$('#lPass').on('focus', function() {
    if (this.value == this.defaultValue) this.value = '';
    $(this).after('<input type="password" id="lPass" size="10" value="' + this.value + '"/>').remove();
});


$('#lPass').on('blur', function() {
    alert(1);
});
like image 154
Amr Elgarhy Avatar answered Nov 09 '22 12:11

Amr Elgarhy