I’m gettig a bit lost with some javascript logic.
I have a form with 3 text fields.
They show a default value through a javascript (no placeholder).
When the form is focused, the value is cleared and when the form is blurred, the default value is restored if the field value hasn’t been change.
I have to write some conditional logic to ensure that the field text color is changed from grey (default placeholder value) to black if the value is changed.
The 3 fields have a different default value. I tried to write a general script on Blur to check the value of the current field to test it against the default value.
$fields = $('#webform-component-group-1 .webform-component-watermarked');
var keys = [];
var values = [];
console.log($fields.length);
$fields.each(function(){
keys.push($(this).attr('id'));
values.push($(this).val());
$(this).blur(function(){
console.log($(this).val());
console.log($(this).val() !== values[keys.indexOf($(this).attr('id'))]);
});
});
but my test always return true as the placeholder value is not restored when I run the test on blur.
What would be the best way to handle this problem?
Cheers
Using jQuery, you could do it like this:
Assuming the following HTML
JavaScript
CSS
Fiddle: http://jsfiddle.net/uWPJC/