8 Replies - 957 Views - Last Post: 18 May 2010 - 01:48 PM

#1 Guest_OnePig2RuleThemAll*


Reputation:

2 shorthand javascript questions

Posted 16 May 2010 - 10:28 AM

Hey all, I was wondering if anyone knew the answers to these two shorthand javascript questions:

1) Is there anything wrong with doing this:
'd' = document.getElementById;
and then just using d('someID') throughout the rest of the page as I need it? I suppose I could write a function which returns the value, but why write a function if the above is completely fine is what I am thinking. Your input is greatly appreciated.

2) can anyone think of a more efficient way to write this:
return someString.replace(/a/g, "f").replace(/b/g, "g").replace(/c/g, "h").replace(/d/g, "i").replace(/e/g, "j");

I tried really hard to think of how I could use a function for the replacement letter, but the only functions I could come up with were not efficient enough to make up for replacing the 4 extra ".replace" and "//g,".

Thanks a bunch.

Is This A Good Question/Topic? 0

Replies To: 2 shorthand javascript questions

#2 Fratyr  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 139
  • Joined: 10-April 08

Re: 2 shorthand javascript questions

Posted 17 May 2010 - 01:48 AM

1) Wrong code ..
'd' = document.getElementById;



You can't assign something to.... string!?

var d = document.getElementById;


Is better, as we create variable.

Not sure if it will work like that: d('someID')

But you should try it.
Was This Post Helpful? 1
  • +
  • -

#3 teampoop  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 140
  • Joined: 19-June 08

Re: 2 shorthand javascript questions

Posted 17 May 2010 - 08:30 AM

I didn't think it would work, because it's not going to accept a parameter. I put the following into Firebug's console, to see what kind of error it would throw:

var d = document.getElementById;
var x = d('content');
console.debug(x);



Turns out, it didn't throw any error, which means when it tried to debug 'x' it was still null. The proper way to write this would be something like:

var d = function(a){return document.getElementById(a);};


Was This Post Helpful? 0
  • +
  • -

#4 Guest_OnePig2RuleThemAll*


Reputation:

Re: 2 shorthand javascript questions

Posted 17 May 2010 - 10:48 AM

Thaks a bunch guys, I thought there was something not right about what I was doing.
Was This Post Helpful? 0

#5 Guest_OnePig2RuleThemAll*


Reputation:

Re: 2 shorthand javascript questions

Posted 17 May 2010 - 11:13 AM

I just had one more quick question then. Is this code acceptable:
var d = function(a){return document.getElementById(a);};
x = d('content').value;



Would this code be acceptable for making x contain the value of the an input field named 'content' value?

Thanks a bunch for your help
Was This Post Helpful? 0

#6 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 125
  • View blog
  • Posts: 818
  • Joined: 25-October 08

Re: 2 shorthand javascript questions

Posted 17 May 2010 - 06:34 PM

View PostOnePig2RuleThemAll, on 17 May 2010 - 10:13 AM, said:

I just had one more quick question then. Is this code acceptable:
var d = function(a){return document.getElementById(a);};
x = d('content').value;



Would this code be acceptable for making x contain the value of the an input field named 'content' value?

Thanks a bunch for your help


Yes.
Was This Post Helpful? 1
  • +
  • -

#7 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1107
  • View blog
  • Posts: 6,924
  • Joined: 07-September 06

Re: 2 shorthand javascript questions

Posted 17 May 2010 - 07:56 PM

View PostOnePig2RuleThemAll, on 17 May 2010 - 05:13 PM, said:

I just had one more quick question then. Is this code acceptable:
var d = function(a){return document.getElementById(a);};
x = d('content').value;



Would this code be acceptable for making x contain the value of the an input field named 'content' value?

Thanks a bunch for your help


That will acutally depend on what you mean by "named". If you are saying that you used the name attribute to give the input the name "content", then no. However if you mean that you gave the input the ID attribute "content" then yes.

document.getElementById() does exactly that, gets an element by the specified ID, nothing else is looked at.
Was This Post Helpful? 1
  • +
  • -

#8 Guest_OnePig2RuleThemAll*


Reputation:

Re: 2 shorthand javascript questions

Posted 17 May 2010 - 09:03 PM

thank you very much for your help. I was uncertain if there was an error with the technique or not. Having your guys help has made my day, thanks a bunch.
Was This Post Helpful? 0

#9 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 125
  • View blog
  • Posts: 818
  • Joined: 25-October 08

Re: 2 shorthand javascript questions

Posted 18 May 2010 - 01:48 PM

View PostOnePig2RuleThemAll, on 17 May 2010 - 08:03 PM, said:

thank you very much for your help. I was uncertain if there was an error with the technique or not. Having your guys help has made my day, thanks a bunch.


Often times you can create a small test program to try your code.
By small, I mean try to keep your commands to those that test your concept
and allows for input and output displays to see the effects of the command.

If it does what you expect and does not cause errors in the error console,
I would deem it to be a "success" and feel confident to use it further
(or until some browser incompatibility shows up :bigsmile: ).

Good Luck!
:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1