I'm trying to simplify my jQuery code by not having to include my object id more than once per if statement. I've tried a few solutions but none seem to do the trick. Basically I have a form for registration and an if statement during submission to check if there are any problems with the field (it's blank, for instance). I just want to have the if statement refer to the input value, and if it's blank, use "this" to give focus back to the input. Here's my code so far:
if ($("#first_name").val()=="") {
$("#first_name").focus();
return false;
}
This works as is, but I'd like to get rid of the second reference to #first_name and just use "this" instead. Is this possible? Ideally it would look something like this:
if ($("#first_name").val()=="") {
$(this).focus();
return false;
}
I know it doesn't work because the if statement is looking at val(), how do I make it look at the object instead?
Aucun commentaire:
Enregistrer un commentaire