$('#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?
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.
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.
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.
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.
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With