Jquery Best Practice - Putting DOM Element in Variable regardless of how many times it was used

Wesley Brian Lachenal

While browsing for some best practices on Jquery, I stumbled into this one.

Reference: http://gregfranko.com/jquery-best-practices/#/13

// Stores the live DOM element inside of a variable
  var elem = $("#elem");

  // Set's an element's title attribute using it's current text
  elem.attr("title", elem.text());

  // Set's an element's text color to red
  elem.css("color", "red");

  // Makes the element fade out
  elem.fadeOut();

It says it's the best to CACHE YOUR SELECTORS IN VARIABLES

Then while working, I thought to myself, do I really have to put #current-contact-info and #list-contact-form on a variable?

Since I'm just calling it once, does it affect the performance?

Option 1:

$("#edit-contact-list").on("click", function() {
    $("#current-contact-info").addClass("hide");
    $("#list-contact-form").removeClass("hide");
});

Or option 2 is the best way regardless of how many times it was called?

Option 2:

$("#edit-contact-list").on("click", function() {

    var current_contact_info = $("#current-contact-info");
    var list_contact_form = $("#list-contact-form");

    current_contact_info.addClass("hide");
    list_contact_form.removeClass("hide");
});

I was kind of insecure on the amount of line of code though.

xyz

Always consider simplicity and readability along with performance/best practices. Storing a element in variable is of no use if it will be used only once, also it wont help in making code more simple/readable.
I always prefer

 $("#current-contact-info");.addClass("hide");
 $("#list-contact-form").removeClass("hide");

over

var current_contact_info = $("#current-contact-info");
var list_contact_form = $("#list-contact-form");

current_contact_info.addClass("hide");
list_contact_form.removeClass("hide");

If you are worried about lines of code (it deosnt really matter though), you can achieve it in 1 line. Use toggleClass over addClass/removeClass and use OR operator in selector

 $("#current-contact-info, #list-contact-form").toggleClass("hide");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is it possible for a macro to expand to code that will be executed exactly once when a program starts, regardless of how many times it's used?

From Dev

Best practice for calling save() many times

From Dev

How to count how many times a global variable was used (read and write)?

From Dev

Admob best practice: how many times do I request, and how long should I show a banner ad?

From Dev

Best practice: how to pass many arguments to a function?

From Dev

jQuery - check if variable is dom element

From Java

How many times the word is used on the html page

From Dev

Is there a formula that will count how many times "@" is used in a cell?

From Dev

How to use @JsonManagedReference and @JsonBackReference for Many to Many relationship with best practice?

From Dev

Counting how many times variable has changed

From Javascript

Console log how many times an object in an array appears regardless of the case of the characters within it

From Dev

jQuery, which is best, assigning element to variable or not?

From Dev

jQuery target specific DOM element based on variable

From Dev

Checking if jQuery variable is referring to a specific DOM element

From Dev

How to find how many times a code was used in the past 5 years

From Dev

Best practice for variable saving

From Dev

Initialize a variable with a DOM element: is the "window.onload" best for it?

From Dev

Python How many times each meta keyword is used in a string

From Dev

Counting how many times a base function is being used, Python

From Dev

Does MongoDB track how many times each index is used in a query?

From Dev

PHP Count how many times code is used on a page

From Dev

Counting how many times the click() function has been used

From Dev

Find to Positions of Characters and How Many Times Used in Text with Python

From Dev

How many times split has been used in breaking a sentence

From Dev

Manipulating DOM in angularJS : best practice?

From Dev

SQL one to many, best practice?

From Javascript

jQuery Standards and Best Practice

From Dev

best-practice for ordered many to many relation?

From Dev

How to determine how many times an element is present in a list?

Related Related

  1. 1

    Is it possible for a macro to expand to code that will be executed exactly once when a program starts, regardless of how many times it's used?

  2. 2

    Best practice for calling save() many times

  3. 3

    How to count how many times a global variable was used (read and write)?

  4. 4

    Admob best practice: how many times do I request, and how long should I show a banner ad?

  5. 5

    Best practice: how to pass many arguments to a function?

  6. 6

    jQuery - check if variable is dom element

  7. 7

    How many times the word is used on the html page

  8. 8

    Is there a formula that will count how many times "@" is used in a cell?

  9. 9

    How to use @JsonManagedReference and @JsonBackReference for Many to Many relationship with best practice?

  10. 10

    Counting how many times variable has changed

  11. 11

    Console log how many times an object in an array appears regardless of the case of the characters within it

  12. 12

    jQuery, which is best, assigning element to variable or not?

  13. 13

    jQuery target specific DOM element based on variable

  14. 14

    Checking if jQuery variable is referring to a specific DOM element

  15. 15

    How to find how many times a code was used in the past 5 years

  16. 16

    Best practice for variable saving

  17. 17

    Initialize a variable with a DOM element: is the "window.onload" best for it?

  18. 18

    Python How many times each meta keyword is used in a string

  19. 19

    Counting how many times a base function is being used, Python

  20. 20

    Does MongoDB track how many times each index is used in a query?

  21. 21

    PHP Count how many times code is used on a page

  22. 22

    Counting how many times the click() function has been used

  23. 23

    Find to Positions of Characters and How Many Times Used in Text with Python

  24. 24

    How many times split has been used in breaking a sentence

  25. 25

    Manipulating DOM in angularJS : best practice?

  26. 26

    SQL one to many, best practice?

  27. 27

    jQuery Standards and Best Practice

  28. 28

    best-practice for ordered many to many relation?

  29. 29

    How to determine how many times an element is present in a list?

HotTag

Archive