I have three form fields: name, first name, and lastname. The name field is hidden and I'm trying to set the value to the firstname + lastname on keypress. I'm also trying to format it so that the characters are all lowercase and spaces are transformed into dashes.
So:
<input type="text" name="firstname" value="John John" />
<input type="text" name="lastname" value="Doe" />
<input type="hidden" name="name" value="john-john-doe" />
I've been trying to use the following code, but it's not working...
$('input[name=field_firstname[0][value]]').keyup(function() {
var firstname = $('input[name=field_firstname[0][value]]').val();
var lastname = $('input[name=field_lastname[0][value]]').val();
$('input[name=name]').val(firstname.replace(/\s/g, "-").toLowerCase()+lastname.replace(/\s/g, "-").toLowerCase());
});
$('input[name=field_lastname[0][value]]').keyup(function() {
var firstname = $('input[name=field_firstname[0][value]]').val();
var lastname = $('input[name=field_lastname[0][value]]').val();
$('input[name=name]').val(firstname.replace(/\s/g, "-").toLowerCase()+lastname.replace(/\s/g, "-").toLowerCase());
});
idinstead ofname. The jQuery syntax would be much clearer: that would be$('input#firstname')instead of$('input[name="firstname"]'), for instance.