Posts: 876
Name: Matt Pealing
Location: England, north west
|
I have the following code that I'm using to cycle through each element in a form field, and clear whatever text is in it. If the user clicks out of the field without typing anything then the script is supposed to return to whatever value it started with (e.g. "Enter your name..."). If the user does type something, then it stays in the field even when they click out of it:
Code:
// clear the text in the form fields
$(document).ready(function() {
$('#frm-quick input').focus(function() {
var fieldFocus = 0;
var fieldVal = $(this).val();
if (fieldFocus == 0) {
$(this).val('');
}
$(this).keydown(function() {
fieldFocus = 1;
})
})
$(this).blur(function() {
if (fieldFocus == 0) {
$(this).val(fieldVal);
}
})
})
I can see that fieldVal and fieldFocus will be overwritten each time the loop does a cycle. Meaning their values will be meaningless.
What I want to do is use an array. I know how to do this in traditional JavaScript no problem (e.g. something like fieldVal[i] = ...), but how would I do it in jQuery?? As the loop seems to be 'automatic'.
|