Reputation: 667
I am a big fan of keeping my code clean and tidy, I am very much a beginner to javascript however. I was wondering if the following code could be written better/cleaner:
<script language="javascript" type="text/javascript">
$('.clickMe').click(function() {
$('.refine_icon').toggleClass("opened");
$('.advancedSearch').slideToggle(400);
$(this).text($(this).text() == 'More search options' ? 'Fewer search options' : 'More search options');
return false;
});
</script>
Upvotes: 0
Views: 628
Reputation: 93684
The code looks fine, and for something simple can't be improved a great deal. I would use proper indenting though:
$('.clickMe').click(function() {
$('.refine_icon').toggleClass("opened");
$('.advancedSearch').slideToggle(400);
$(this).text($(this).text() == 'More search options' ? 'Fewer search options' : 'More search options');
return false;
});
One thing you could do is replace return false;
with jQuery's preventDefault()
. Returning false
not only stops the default behaviour of the browser, but prevents the event from bubbling up the DOM tree. Usually this isn't a problem, but if you're interested in writing code that has minimal side effects, then this is the way to go:
$('.clickMe').click(function (e) { // Notice the e
e.preventDefault();
// Rest of your code
});
jQuery Fundamentals is a comprehensive online book that explains the basics of both JavaScript and jQuery (with a focus on jQuery though).
Upvotes: 2