Jump to content
Sign in to follow this  
w3schoon

Set JQuery methods in a variables for optimisation

Recommended Posts

Nice be with you everyone! After I've learned setting JQuery selectors in a variables for optimisation.Example:

		   $(document).ready(function(){				$("form").submit(function(){					var a=$("#position"); var b=$("#job"); var c=$("input#religion"); var d="Others";										if (a.val() == "" && b.val() == d ){						b.val("").focus();						return false;					}					if (c.val() == d){						c.val("").focus();						return false;					}				});			});  

My related question is: Can I set JQuery methods in a variables for optimisation?Example:

			$(document).ready(function(){				var a=attr("maxlength", 32);				$("#specialty").a;			});

Thank you.

Edited by w3schoon

Share this post


Link to post
Share on other sites

It should also be noted that putting a reference to something in a variable doesn't automatically make it more efficient. In your original code you only use the variable a once, so there's no reason to save that in a variable. It's not more efficient, in fact it now uses memory to save that object in a variable reference when you only needed to refer to it once. You use b and c each potentially twice, so saving that reference in a variable might make it more efficient because it doesn't need to look up the element in the DOM more than once. You're still using the val method more than once though, so it would be better to save the value instead of the element if you only need to refer to the value, the value will use less memory.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...