PDA

View Full Version : Resolved using the value of a string variable as part of ??



ksz
03-21-2012, 06:05 PM
Since I don't know the right lingo in stating my question, I was not able to find an answer to my problem on the web.
I am trying to take the value of a string variable and use it as part of ____
I don't know the right lingo to fill in the blank

Here are two examples:
Example 1:
var temp = 'document';
var tooltip = temp.getElementById('tipdiv');

1- Am I correct in my assignment of tooltip above?
meaning tootip= document.getElementById('tipdiv')

2- Can I say that if
temp.getElementById('tipdiv').style.width = '50px'
is the same as
tooltip.style.width = '50px'
also the same as
document.getElementById('tipdiv').style.width = '50px'

Example 2:
var temp = 'style';
var tooltip = document.getElementById('tipdiv');

Am I correct to say:
1- tooltip.temp.width = '50px';
is the same as
tooltip.style.width = '50px';

I have tried the first example in using the string variable in a conditional statement and it seems to work. such as
var temp = 'document'; then used it in
if (temp.all) and it worked
but it didn't work when I set
var temp = 'width'; then used it in assigning value, such as
document.getElementById('tipdiv').style.temp = '50px';

I am trying to learn how to construct a part of or whole statement (I don't think the word "statement' is the correct lingo) such as
document.getElementById('tipdiv').style.width from string variables

Thank you guys

jscheuer1
03-21-2012, 07:17 PM
First of all, 'stringifying' javascript should be avoided wherever possible. It generally leads to more errors than hard code does. You cannot really do the first example without eval() which is hokey. You could do it without a string/quotes though:


var temp = document;

With style you can do it with [] notation:



var temp = 'style';
var tooltip = document.getElementById('tipdiv');
tooltip[temp].width = '50px';
//is the same as
tooltip.style.width = '50px';

There's little value in doing that though. But with individual properties of style it can be handy in situations where you don't know the style property you want to set ahead of time. A more common way to use style in a situation like that is:


var tooltip = document.getElementById('tipdiv');
var temp = tooltip.style;
temp.width = '50px';
//is the same as
tooltip.style.width = '50px';

as many style declarations for tooltip can be made with minimal typing.

ksz
03-21-2012, 08:12 PM
I appreciate the quick and thorough response. I was asking for learning purposes. So I still would like to know, even if it s not recommended:

var temp = 'document'; // with string/quotes
var tooltip = temp.getElementById('tipdiv');

Is this the same as if
var temp = document; // without string/quotes
var tooltip = temp.getElementById('tipdiv');

--------

Also, is valid to say:
var temp = 'document'; // with string/quotes
var elmt = getElementById('tipdiv');
vat tooltip = temp[elmt];

Thank you

Nile
03-21-2012, 10:47 PM
The following is the correct way to do it. Using quotes around "document" would most likely result in an error.


var temp = document; // without string/quotes
var tooltip = temp.getElementById('tipdiv');


Your second example:


var temp = 'document'; // with string/quotes
var elmt = getElementById('tipdiv');
vat tooltip = temp[elmt];

Will not work because, for one, document is in single quotes, telling javascript that it's a string, when really it's an object. For two, because "getElementById()" is a function, not an object, and also because getElementById() depends on another element to run.

jscheuer1
03-21-2012, 11:42 PM
I appreciate the quick and thorough response. I was asking for learning purposes. So I still would like to know, even if it s not recommended:

var temp = 'document'; // with string/quotes
var tooltip = temp.getElementById('tipdiv');

Is this the same as if
var temp = document; // without string/quotes
var tooltip = temp.getElementById('tipdiv');

--------

Also, is valid to say:
var temp = 'document'; // with string/quotes
var elmt = getElementById('tipdiv');
vat tooltip = temp[elmt];

Thank you

No and No.

And Nile is right, mostly. Except getElementById is only valid when preceded by document. or an equivalent variable or expression representing the document object. Unlike some plural getElements . . . (byClassName, byTagName), functions in javascript, which can be functions of other elements. And this is rightly so, since according to standards there can be only one element with a given id in a given document.

One thing I missed though before. Since document is a property of the window object, this would work:


var temp = 'document';
var tip = window[temp].getElementById('tipdiv');

ksz
03-23-2012, 12:24 AM
Thank you guys