Many developers ignore shortcuts and power of jQuery and I myself being a victim of some of the useful, handy but yet ignored shortcuts of jQuery. These are not some advance or complex shortcuts but I guess these are used a lot. 

 

Consider, below jQuery code lines.

1

$("#elm").css("display""none"); //Line 1

2

$("#elm").css("display"""); //Line 2

3

$("#elm").html(""); //Line 3

Above code lines are very common but its unfortunate because some faster and more readable shortcuts available, yet ignored. Find below jQuery code lines that does the exactly same thing but these are more readable and fast.

1

$("#elm").hide(); //Line 1

2

$("#elm").show(); //Line 2

3

$("#elm").empty(); //Line 3

There are many more examples which you can find in your code. For example, to add CSS Class to any element

1

$("#elm").prop("class""cssClass");

where the better approach would be,

1

$("#elm").addClass("cssClass");

Another example is of setting height and width,

1

$("#elm").css("height""100px"); //Line 1

2

$("#elm").css("width""100px"); //Line 2

using in-built jQuery functions,

1

$("#elm").height(100); //Line 1

2

$("#elm").width(100); //Line 2

Another common situation is of toggling (show/hide) of element. For example, 

1

if($("#elm").is(":visible"))

2

    $("#elm").css("display""none");

3

else

4

    $("#elm").css("display""");

where the better approach is,

1

$("#elm").toggle();

You can find many such situation where in-built jQuery functions can be used. Hope you find this useful and if you are also ignoring these shortcuts, then you need to move on…


Good Luck….

Advertisements