Reputation: 73
On my inputs I have a function which remove the value of the inputs when the user click on it. For that, no problem.
I'm trying to write a function to "rewrite" in the input fields, the last known value if the user didn't fill it on blur.
It works fine when I have only one input using the following code :
$("input").blur(function() {
if ($(this).val().length != 0){}
else{
$("#angleplansup1").val(parameters5.a1);
}
}
);
parameters5.a1 = Number($('#angleplansup1').val());
The problem is I have a lot of inputs and It doesn't work when I put all off them because, they all change... (that's logical).
So, and I'm searching a way to put the last known value.
$("input").blur(function() {
if ($(this).val().length != 0){}
else{
$(this).val("the last known value");
}
}
);
I tried unsuccesfully to use document.activeElement
but it doesn't seems to work with blur.
Another way would be to automatize a kind of switch or array, but I'm not skilled enough to think on it yet and I would have your view on this. Here is my code with other variables if it can help. Thank you very much. Have a nice day.
$("input").blur(function() {
if ($(this).val().length != 0){}
else{
$("#hauteuraxe1").val(parameters5.h);
$("#angleplansup1").val(parameters5.a1);
$("#grandehauteur2").val(parameters5.gh);
$("#delta2").val(parameters5.delta);
$("#petitehauteur3").val(parameters5.ph);
...
..
.
}
});
parameters5.h = Number($('#hauteuraxe1').val());
parameters5.a1 = Number($('#angleplansup1').val());
parameters5.gh = Number($('#grandehauteur2').val());
parameters5.delta = Number($('#delta2').val());
parameters5.ph = Number($('#petitehauteur3').val());
...
..
.
Upvotes: 0
Views: 135
Reputation: 68675
For first you need to hold your values. For that I create an object values
which holds the values like 'id' : 'value"
. After every blur
if there is a value, I store the text in the values
. If after blur
there is no value, I set the previous one.
See the example
var values = {};
$('input').on('blur', function(e){
var target = e.currentTarget;
if(target.value){
values[target.id] = target.value
} else {
target.value = values[target.id];
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="a" />
<input id="b" />
Upvotes: 1