locked
Document.getElementById method parameter - single quotes or double quotes? RRS feed

  • Question

  • When using the document.getElementById() method, should the parameter passed to it (if it is an attribute like "new_bankholidaystart") inside its parentheses be contained in single quotes or double quotes?  I have looked online and have found conflicting information.  I don't have a way of testing it either and that is the reason that I am asking.  Thanks!
    Wednesday, January 11, 2012 2:52 PM

Answers

  • javascript tends to treat the two interchangeably.  The getElementById() function is meant to take a string value it is ok to use double quotes.  If there is a chance the function argument wants an object reference you may want to use single quotes.
    • Proposed as answer by Faisal Fiaz Wednesday, January 11, 2012 5:02 PM
    • Marked as answer by wikky2007 Wednesday, January 11, 2012 9:29 PM
    Wednesday, January 11, 2012 4:36 PM

All replies

  • javascript tends to treat the two interchangeably.  The getElementById() function is meant to take a string value it is ok to use double quotes.  If there is a chance the function argument wants an object reference you may want to use single quotes.
    • Proposed as answer by Faisal Fiaz Wednesday, January 11, 2012 5:02 PM
    • Marked as answer by wikky2007 Wednesday, January 11, 2012 9:29 PM
    Wednesday, January 11, 2012 4:36 PM
  • What do you mean by "object reference"?  Can you please elaborate?  I am sort of new to this stuff.  Thanks

    Wednesday, January 11, 2012 5:10 PM
  • As its name implies, javascript is a script-based language.  That means nothing is pre-compiled and arguments are passed around as generic variants (var)s.  These variants could be a literal value such as a numeric 1,2,3... or could be a reference to an object that exists on an HTML page, or that was created in script (object reference).  Javascript tries to interpret the argument type according to what the function is trying to do with it.  Thus the function window.alert(1);  and window.alert("1"); produce the same result, since the script engine chooses to interpret both as a string value.  The function window.alert(1+"1"); results in the alert message "11" while the function window.alert(1+1) results in the alert message "2" because the script engine interprets the values as numeric, adds them up, then interprets the result as a string and displays that.

    http://www.w3schools.com/js/

    -JayB

    Wednesday, January 11, 2012 5:24 PM