Consider, below jQuery code lines.
$("#elm").css("display", "none"); //Line 1Above 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.
$("#elm").css("display", ""); //Line 2
$("#elm").html(""); //Line 3
$("#elm").hide(); //Line 1Related Post: There are many more examples which you can find in your code. For example, to add CSS Class to any element
$("#elm").show(); //Line 2
$("#elm").empty(); //Line 3
$("#elm").prop("class", "cssClass");where the better approach would be,
$("#elm").addClass("cssClass");Another example is of setting height and width,
$("#elm").css("height", "100px"); //Line 1using in-built jQuery functions,
$("#elm").css("width", "100px"); //Line 2
$("#elm").height(100); //Line 1Another common situation is of toggling (show/hide) of element. For example,
$("#elm").width(100); //Line 2
if($("#elm").is(":visible"))where the better approach is,
$("#elm").css("display", "none");
else
$("#elm").css("display", "");
$("#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...
Feel free to contact me for any help related to jQuery, I will gladly help you.
No comments:
Post a Comment