I'm having a little problem with my script below. I'm trying to create a script where you can write some text in an input field, and when you have typed some text, you will get an alert when focus-out. The alert should only show, if the input contain text.
But the problem is, that if you're trying to write some text, delete it and then focus-out of the input, the alert do not show next time, when you actually have written something and then focus-out.
Right now, the alert function always will "disappear" when focus out, no matter if you have written any thing or not.
I have tried to add a var already_alert_me_once = true
before the alert, and then put everything inside an: if(already_alert_me_once == false
), but that didn't do the trick.
I have also tried to change $('#title').focusout(function()
with $('#title').one('focusout', function()
which almost did the trick.
Here is my current script:
// When focus on title
$('#title').focus(function () {
// Check if empty
if (!$(this).val()) {
$('#title').one('focusout', function () {
if ($(this).val()) {
alert("YEP");
}
});
}
});
..and Here's a Fiddle
So my question is; How to I do so the alert only appears when you have written something, and after that never again (unless you reload the page).
Hope you can understand what I mean.
Thanks - TheYaXxE
I'm having a little problem with my script below. I'm trying to create a script where you can write some text in an input field, and when you have typed some text, you will get an alert when focus-out. The alert should only show, if the input contain text.
But the problem is, that if you're trying to write some text, delete it and then focus-out of the input, the alert do not show next time, when you actually have written something and then focus-out.
Right now, the alert function always will "disappear" when focus out, no matter if you have written any thing or not.
I have tried to add a var already_alert_me_once = true
before the alert, and then put everything inside an: if(already_alert_me_once == false
), but that didn't do the trick.
I have also tried to change $('#title').focusout(function()
with $('#title').one('focusout', function()
which almost did the trick.
Here is my current script:
// When focus on title
$('#title').focus(function () {
// Check if empty
if (!$(this).val()) {
$('#title').one('focusout', function () {
if ($(this).val()) {
alert("YEP");
}
});
}
});
..and Here's a Fiddle
So my question is; How to I do so the alert only appears when you have written something, and after that never again (unless you reload the page).
Hope you can understand what I mean.
Thanks - TheYaXxE
Share Improve this question edited Feb 5, 2014 at 13:52 Yuvi 1852 silver badges10 bronze badges asked Feb 5, 2014 at 13:49 TheYaXxETheYaXxE 4,2943 gold badges24 silver badges35 bronze badges 4-
Typo, should say
on
notone
– Tom Styles Commented Feb 5, 2014 at 13:53 -
1
@TomStyles
one
means it only gets triggered once. – Reinstate Monica Cellio Commented Feb 5, 2014 at 13:56 - jsfiddle/rkumar670/T7tFd/6 – Rahul Kumar Commented Feb 5, 2014 at 13:57
- 1 Learning everyday. Thanks for the correction @Archer – Tom Styles Commented Feb 5, 2014 at 16:05
3 Answers
Reset to default 3You can unbind the focus using:
$('#title').unbind('focus');
Working Fiddle
pretty much the easiest solution, no need for .one or focus events :)
$('#title').blur(function() {
if( $(this).val() ) {
alert('!!');
$(this).unbind('blur');
}
});
http://jsfiddle/T7tFd/5/
instead of putting the variable "already_alert_me_once" inside the function, make it a global variable. that way the scope will be maintained. you could also add an attribute to the #title that indicates that it has already been clicked, then check to see if that attribute exists before executing the code.