petewall Posted February 11, 2012 Share Posted February 11, 2012 Hi, i've recently read a bit about jQuery, and started to write some code today, this is what i came up with so far: $("#mydiv").click(function() { if($(".myclass").is(":hidden")) { $(".myclass").slideDown("slow"); } else { $(".myclass").slideUp("slow"); } }); superbasic stuff that slides down a <div> on click. well, i'd love some tips on what to do next, something not to advanced and maybe even some pointers on what to look at to get the script done. Quote Link to comment https://forums.phpfreaks.com/topic/256856-new-to-jquery-tips-on-what-to-start-with/ Share on other sites More sharing options...
trq Posted February 11, 2012 Share Posted February 11, 2012 The first thing that you should learn is about selectors. Your already using them in your example, but very inefficiently. Every time you use $('someselector') jQuery searches the DOM for that element, therefore, if your going to use the same element more than once, save it locally. $("#mydiv").click(function() { $myclass = $(".myclass"); if ($myclass.is(":hidden")) { $myclass.slideDown("slow"); } else { $myclass.slideUp("slow"); } }); Quote Link to comment https://forums.phpfreaks.com/topic/256856-new-to-jquery-tips-on-what-to-start-with/#findComment-1316906 Share on other sites More sharing options...
petewall Posted February 11, 2012 Author Share Posted February 11, 2012 alright, does it work the same if i declare it outside the function? like $myclass = $(".myclass"); and then $("#mydiv").click(function() { if ($myclass.is(":hidden")) { $myclass.slideDown("slow"); } else { $myclass.slideUp("slow"); } }); or does it have to be within the function? Quote Link to comment https://forums.phpfreaks.com/topic/256856-new-to-jquery-tips-on-what-to-start-with/#findComment-1317065 Share on other sites More sharing options...
Adam Posted February 11, 2012 Share Posted February 11, 2012 Yeah that's fine. Unlike with PHP where you have to declare variables inside a function as global, JS automatically has scope of global variables. Adding to what thorpe said, you can also improve the performance of a selector by adding the element tag ("div.myclass" instead of ".myclass" for example), as it reduces the number of elements to check. Quote Link to comment https://forums.phpfreaks.com/topic/256856-new-to-jquery-tips-on-what-to-start-with/#findComment-1317118 Share on other sites More sharing options...
petewall Posted February 13, 2012 Author Share Posted February 13, 2012 alright, but anyone got any suggestions on easy scripts that's good to start with for a beginner like me? greatly appreciated Quote Link to comment https://forums.phpfreaks.com/topic/256856-new-to-jquery-tips-on-what-to-start-with/#findComment-1317740 Share on other sites More sharing options...
scootstah Posted February 13, 2012 Share Posted February 13, 2012 Learn Javascript, then use jQuery. Things will be a lot easier and make more sense. Quote Link to comment https://forums.phpfreaks.com/topic/256856-new-to-jquery-tips-on-what-to-start-with/#findComment-1317792 Share on other sites More sharing options...
Adam Posted February 13, 2012 Share Posted February 13, 2012 Learn Javascript, then use jQuery. Things will be a lot easier and make more sense. +1 Quote Link to comment https://forums.phpfreaks.com/topic/256856-new-to-jquery-tips-on-what-to-start-with/#findComment-1317807 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.