Ok first off you should cache the result of the search as otherwise it'll be
performed every time. Then, you can just chain the operations on
input#rxt_forename straight, except for one part. When you modify a jQuery
object, e.g. with addClass(), the function returns the jQuery object. This
chaining is broken if you traverse from that set, e.g. with get(0). When you
use a 'destructive' operation like that, you have to use end() to get the
previous state.

 var rxtForename = $('input#rxt_forename');
 if( rxtForename.val() == '' ){
   // add class to change colour
   rxtForename
     .addClass("required")
     .get(0).focus().end() // end() undoes the last destructive operation,
which in this case
is get(0)
      .siblings("span.error").show();
   return false;
 }else{
   rxtForename
     .removeClass("required")
     .siblings("span.error").hide();
 }

Hope that helps,
Rob


On 3/21/07, Luc Pestille <[EMAIL PROTECTED]> wrote:


 Hi all,
I've got some simple validation to do (not enough to use one of the form
plugins), and have this;

// when the form is submitted
  $('form#checkout').submit( function(){

   // if field has something in it, do submit actions, otherwise write out
error
   if( $('input#rxt_forename').val() == '' ){
    // add class to change colour
    $('input#rxt_forename').addClass("required").get(0).focus();
    $('input#rxt_forename').siblings("span.error").show();
    return false;
   }else{
    $('input#rxt_forename').removeClass("required");
    $('input#rxt_forename').siblings("span.error").hide();
   }

How can i simplify it? I would have thought replacing the input selector
with $(this) would work, but it doesn't, and I'm sure the class/siblings
processes could be chained...

Thanks,



 *Luc Pestille*
Web Designer

e: [EMAIL PROTECTED]
t: +44 (0)1628 899 700
f: +44 (0)1628 899 701



In2
Thames House
Mere Park
Dedmere Road
Marlow
Bucks
SL7 1PB

Tel 01628 899700
Fax 01628 899701
e: [EMAIL PROTECTED]
i: www.in2.co.uk

This message (and any associated files) is intended only for the use of
discuss@jquery.com and may contain information that is confidential,
subject to copyright or constitutes a trade secret. If you are not
discuss@jquery.com you are hereby notified that any dissemination, copying
or distribution of this message, or files associated with this message, is
strictly prohibited. If you have received this message in error, please
notify us immediately by replying to the message and deleting it from your
computer. Messages sent to and from us may be monitored. Any views or
opinions presented are solely those of the author [EMAIL PROTECTED] do not 
necessarily represent those of the company.


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/




--
Rob Desbois
Eml: [EMAIL PROTECTED]
Tel: 01452 760631
Mob: 07946 705987
"There's a whale there's a whale there's a whale fish" he cried, and the
whale was in full view.
...Then ooh welcome. Ahhh. Ooh mug welcome.

GIF image

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to