You don't need the elements in the selector (i.e. form / input). The fastest
way to select is on the unique id. 

 

Also, instead of calling the same jQuery selector over and over set it to a
local variable. 

 

$('#checkout').submit( function(){
            // if field has something in it, do submit actions, otherwise
write out error
            var a = $('#rxt_forename');

if(a.val() == '' ){
            // add class to change colour
                        a.addClass("required").get(0).focus();
                        a.siblings("span.error").show();
                        return false;
            }else{
                        a.removeClass("required");
                        a.siblings("span.error").hide();
            }

 

David Dexter   |  brilliantlemming.com  |  Brentwood, CA  |  310.414.0949

  _____  

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On
Behalf Of Luc Pestille
Sent: Wednesday, March 21, 2007 9:40 AM
To: jQuery Discussion.
Subject: [jQuery] Code simplifying request.

 


 

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 <http://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] and
do not necessarily represent those of the company. 

 

Attachment: image001.gif
Description: GIF image

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

Reply via email to