javascript - How can I refactor this jQuery code? -
The code below is for a simple newsletter signup widget.
I'm sure there is a way to make it more concise, any thoughts?
var email_form = $ ('. Widget_subscribe form'); Var email_submit = $ ('. Widget_subscribe. Submit'); Var email_link = $ ('. Widget_subscribe .email'); // When the page loads email_form.hide (); Hide the email entry form; // Show the form when clicking email link $ (email_link) Click (function () {$ (this) .toggle (); $ (email_form) .toggle (); return returned;}); // Hide the form form, when the form is submitted $ (email_submit) Click (function () {$ (email_link) toggle (); $ (email_form) .toggle ();}); // clear / reset email input on Focus $ ('Input [name = "email"]'. Focus (function () {$ (this) .val ("");}) Blur (function () { If ($ (this) .val () == "") {$ (this) .val ($ (this) [0] .defaultValue)}}});
You have some similar code.
/ Click the $ / email link to show $ (email_link) click (function () {$ (this) .toggle (); $ (email_form) .toggle (); description is false;}) // form Hide the form, when submitted the form $ (Email_submit) Click (function () {$ (email_link) toggle (); $ (email_form) .toggle ();}); This re-apply Can be done so that the equality becomes clear.
// click ( Function () {$ (email_link) Toggle (); $ (email_form). Toggle (); Return Back;}); // Hide the form form, when the form is submitted $ (email_submit) Click (function () {$ (email_link) toggle (); $ (email_form) .toggle ();}); So you can toggle the link and the form in the panel.
var toggleEmailLinkAndForm = function) {$ (Email_link). Toggle (); $ (Email_form) .toggle (); } $ (Email_link). Click (toggleEmailLinkAndForm); $ (Email_submit) .click (toggleEmailLinkAndForm); And as the others have said, you can drop the redundant $ () s.
var toggle email mail and form = function () {email_link.toggle (); Email_form.toggle (); } Email_link.click (toggleEmailLinkAndForm); Email_submit.click (toggleEmailLinkAndForm);
Comments
Post a Comment