I am relatively new to jQuery, and while I am trying to learn, I am so anxious that I am doing things ... well, Iβm not mistaken ... but not necessarily right.
I have a script below that works fine and does what I need. But looking at this, I am not satisfied, is this really the best way to achieve my goal, or am I too picky?
A brief description of what I'm creating: a search form containing a dynamically generated field. Each field has 2 classes to indicate whether it should appear in a quick and / or advanced search form. Two links to switch between the two views.
function searchModes() { $('p.quicksearch-true.advancedsearch-true').show(); $('p.quicksearch-true.advancedsearch-false').show(); $('p.quicksearch-false.advancedsearch-true').hide(); $('p.quicksearch-false.advancedsearch-false').hide(); $('#advanced').show(); $('#quick').hide(); $('#advanced').click(function () { $('p.quicksearch-true.advancedsearch-true').show(); $('p.quicksearch-true.advancedsearch-false').hide(); $('p.quicksearch-false.advancedsearch-true').show(); $('#advanced').toggle(); $('#quick').toggle(); return false; }); $('#quick').click(function () { $('p.quicksearch-true.advancedsearch-true').show(); $('p.quicksearch-true.advancedsearch-false').show(); $('p.quicksearch-false.advancedsearch-true').hide(); $('#advanced').toggle(); $('#quick').toggle(); return false; }); }
HTML is as follows:
<a href="#" id="quick">quick</a><a href="#" id="advanced">advanced</a> <p class="quicksearch-true advancedsearch-true"> Some dynamically generated form field that appears in both quick and advanced search </p> <p class="quicksearch-true advancedsearch-false"> Some dynamically generated form field that appears in quick search only </p> <p class="quicksearch-false advancedsearch-true"> Some dynamically generated form field that appears in advanced search only </p> <p class="quicksearch-false advancedsearch-false"> Should never occur, but if it did, it would remain hidden anyway </p>
So, really, I get any feedback that may come up that may help me try to write Better jQuery.
Very valuable!
source share