I want to prepopulate the value of the two textfields with the field label e.g. Name, Email Address, until the fields are in focus.
I have this working for the Name field:
$('#form-5 label').hide();
// Prefill newsletter name field with label $("form#form-5 input[name='name']").val('Name').focus(function() { // Clear on focus if ($(this).val() == 'Name') { $(this).val(''); } }) .blur(function() { // Put field title back in on blur if ($(this).val() == '') { $(this).val('Name'); } });
Obviously I can repeat this code on the email field as well, but wondered if there is a way to make it a function with the label value not hardcoded so I can use the same technique on forms with many fields.