jQuery 15 Powerful Tips and Tricks for Developers and Web Designer
Today, We want to share with you jQuery 15 Powerful Tips and Tricks for Developers and Web Designer.In this post we will show you jQuery Tips and Tricks, hear for 15 Powerful jQuery Tips and Tricks for Developers we will give you demo and example for implement.
In this post, we will learn about jQuery: 15 Must-Know Tips and Tricks for Web Designers with an example.
jQuery Tips and Tricks for Developers
In this post, we will learn about Powerful jQuery Tips and Tricks for Developers and Designer with an example.
Now in this post, I will explain 15 Powerful jQuery Tips and Tricks for Developers and Designer with appropriate example. This useful techniques are very useful for your effective use of the library and build-in functionality.
jQuery Tips and Tricks With Example
1. Use the Latest Version of jQuery
By making use of latest version of jQuery one can improve the performance of your web site. Each new release of the library introduces optimizations and bug fixes, and most of the time upgrading involves only changing a script tag.
You can even use jQuery directly from Google’s servers, which provide free CDN hosting for a number of JavaScript libraries.
<!-- Include a specific version of jQuery --> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> <!-- Include the latest version in the 1.6 branch --> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
2. Create an Empty jQuery Object
Many a times you may need to create an empty object, and fill it in with the add() method later on.
var container = $([]); container.add(other_element);
This is also the basis for the quickEach() method that you can use as a faster alternative to the default jquery Interview Question for each()
3. The Selector Property
Retrieving DOM elements with jQuery was a finely combination of parsing selector strings, JavaScript loops and inbuilt APIs like getElementById(),getElementsByClassName()Â and getElementsByTagName().
But now,most of browsers support querySelectorAll()
, which understands CSS query selectors and brings a significant performance gain.
you should try to retrieve element in most optimal way. Some users still follow older browsers that force jQuery into traversing the DOM tree, which is slow.
$('li[data-selected="true"] a') // Looks good, but slow one $('li.selected a') // Better way $('#element') // Best way to get any element
Selecting by ‘id’ is the fastest way. If you need to select by class name, prefix it with a tag – $('p.selected')
. These optimizations mainly affect older browsers and mobile devices.
4. The Selector Property
jQuery supports a property which contains the selector that was used to start the chain.
$('#container li:first-child').selector // #container li:first-child $('#container li').filter(':first-child').selector // #container li.filter(:first-child)
Although the examples above target the same element, the selectors are different. The second one is actually invalid – you can not use it as the basis of a new jQuery object. It only shows that the filter method was used to narrow down the collection.
5. Select a Random Element
creating a selector for retrieving a random element.
(function($){ var random = 0; $.expr[':'].random = function(a, i, m, r) { if (i == 0) { random = Math.floor(Math.random() * r.length); } return i == random; }; })(jQuery); // This is how you use it: $('li:random').addClass('glow');
6.Determine the Weight of Your Page
You can find a quick count of the number of DOM elements on your page by running this in your console.
console.log( $('*').length );
7. Turn your Code into a jQuery Plugin
If you spent some time in writing a piece of jQuery code, consider making it into a plugin.
Creating a jQuery plugin couldn’t be easier:
(function($){ $.fn.yourPluginName = function(){ // Your customr code goes here return this; }; })(jQuery);
8. jQuery Objects as Arrays
The result of running a selector is a jQuery object. But, the library makes it look as if you are working with an array by defining index elements and a length of it.
// Get and select all the navigation buttons: var btn = $('#navigation a.button'); // one can loop though the collection: for(var i=0;i<btn.length;i++){ console.log(btn[i]); // A DOM element, not a jQuery object } // We can even slice it: var firstFive = btn.slice(0,5);
If performance is concern then, using a simple for
(or a while
) loop instead of $.each()
, can make your code few times faster.
9. Disabling Right Mouse Click
If you want to disable right click on the web browser, you can use the following line of code:
$(document).ready(function(){ $(document).bind("contextmenu",function(e){ return false; }); });
10. Determine Browser
Suppose you are in requirement of determine the browser type of the browser currently in use. You can use the following lines of code to achive this:
$(document).ready(function() { // If the browser type is Mozilla Firefox if ($.browser.mozilla && $.browser.version >= "1.8" ){ // your code } // If the browser type is Opera if( $.browser.opera) { // your code } // If the web browser type is Safari if( $.browser.safari ) { // your code } // If the web browser type is Chrome if( $.browser.chrome) { // your code } // If the web browser type is Internet Explorer if ($.browser.msie && $.browser.version <= 6 ) { // your code } //If the web browser type is Internet Explorer 6 and above if ($.browser.msie && $.browser.version > 6) { // your code } });
11. Detect Current Mouse Coordinates:
Suppose you want to detect the current mouse coordinates in the web browser in use. you can write the following line of code.
$(document).ready(function() { $().mousemove(function(e) { $('# MouseCoordinates ').html("X Axis Position = " + e.pageX + " and Y Axis Position = " + e.pageY); }); <DIV id=MouseCoordinates></DIV> });
12. Check if an Element Exists:
Using below lines of code you can check whether an element exists or not.
if ($("#ElementToCheck").length) { //The DOM element exists } else { //The DOM element doesn't exist }
13. Check if an Element Is Visible :
Use below lines of code to check visibility of element.
if($(element).is(":visible") == "true") { //The DOM element is visible } else { //The DOM element is invisible }
14. Set a Timer:
The blow lines of code can be used to set a timer using jQuery:
$(document).ready(function() { window.setTimeout(function() { // some code for timer. }, 500); });
15. jQuery’s Chaining Feature:
Chaining is a powerful feature in jQuery that enable you to chain method calls. Here is an example:
$('someElement').removeClass('classToBeRemovedFromElement').addClass('classToBeAddedInElement');
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about 15 Powerful jQuery Tips and Tricks for Developers.I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.