Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    36
    Thanks
    9
    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;

    or

    documentGetElementById("inputbox").onkeypress = startfunction;

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

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,603
    Thanks
    0
    Thanked 645 Times in 635 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.
    Stephen
    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.

  • 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
    •