Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2012
    Thanked 1 Time in 1 Post

    Two (simple) question

    For say documentGetElementById("inputbox")

    Should we use double or single quotes?

    And is

    var bob = documentGetElementById("inputbox");
    bob.onkeypress = startfunction;


    documentGetElementById("inputbox").onkeypress = startfunction;

    better? Is the second better because it eliminates a variable?

  2. #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Sydney, Australia
    Thanked 932 Times in 919 Posts
    Either " or ' can be used - in JavaScript they are identical. The only time it makes a difference is if the string contains quotes - then you'd use whichever results in you having the fewest quotes in the string needing to be escaped.

    Apart from the typo in your second question where you have G where you should have .g - if there is only the one reference to the field I'd use the single statement. The first version is the more useful one once you have more than one reference to document.getElementById('inputbox') since using the separate variable means that dot only needs to be evaluated once - plus the following code is shorter.
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  3. Users who have thanked felgall for this post:

    cord (06-14-2012)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts