Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-14-2012, 03:05 AM   PM User | #1
cord
New Coder

 
Join Date: Jun 2012
Posts: 36
Thanks: 9
Thanked 1 Time in 1 Post
cord is an unknown quantity at this point
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?
cord is offline   Reply With Quote
Old 06-14-2012, 03:29 AM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,462
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is online now   Reply With Quote
Users who have thanked felgall for this post:
cord (06-14-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:13 AM.


Advertisement
Log in to turn off these ads.