Results 1 to 6 of 6

Thread: using the value of a string variable as part of ??

  1. #1
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default using the value of a string variable as part of ??

    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
    Last edited by ksz; 03-30-2012 at 10:19 PM.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    var temp = document;
    With style you can do it with [] notation:

    Code:
     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:

    Code:
     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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    The following is the correct way to do it. Using quotes around "document" would most likely result in an error.
    Code:
    var temp = document; // without string/quotes
    var tooltip = temp.getElementById('tipdiv');
    Your second example:
    Code:
    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.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by ksz View Post
    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:

    Code:
    var temp = 'document';
    var tip = window[temp].getElementById('tipdiv');
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you guys

Bookmarks

Posting Permissions

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