SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Sep 2002
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quick Javascript Variable Question

    Hi All,

    In the following script I am using a variable named "template" passed through the function as a portion of an element id.
    PHP Code:
    function switchTemplate(templatename) {
    if (
    name == 'Header0'
    {
    template.value=document.theform(name).value;
    document.all.Selected template ID.value='Template Name';
    document.all.NewHeaderDiv.style.display='inline';
    document.all.SelectedHeaderDiv.style.display='none';
    doRenderHTML();

    else 
    {
    template.value=document.theform(name).value;
    document.all.NewHeaderDiv.style.display='none';
    document.all.SelectedHeaderDiv.style.display='inline';
    document.all.SelectedHeaderID.value=document.theform(name).id;
    doRenderHTML();
    }

    The attempt to use the variable is on this line:
    PHP Code:
    document.all.Selected template ID.value='Template Name'
    For example if template = Header, I need the above code to output:
    PHP Code:
    document.all.SelectedHeaderID.value='Template Name'
    How do I properly use the variable "template" in this situation?

    Thanks in advance,
    David
    Last edited by dcbraund; Aug 14, 2003 at 19:56.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi David,

    Code:
    var ele, idString = 'Selected' + template + 'ID';
    if (document.getElementById) {
      ele = document.getElementById(idString);
    }
    else if (document.all) {
      ele = document.all[idString]; 
    }
    ele.value='Template Name';

  3. #3
    SitePoint Zealot
    Join Date
    Sep 2002
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mike,

    I tried your suggestion but ele is coming up as "null" on the following line:
    ele.value='Template Name';
    What do you think I am doing wrong?

  4. #4
    SitePoint Wizard davidjmedlock's Avatar
    Join Date
    Dec 2002
    Location
    Nashville, TN USA
    Posts
    1,688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What browser are you using? Older browsers won't recognize (document.GetElementById)...

  5. #5
    SitePoint Zealot
    Join Date
    Sep 2002
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm Using IE6 on Win XP Pro

  6. #6
    SitePoint Zealot
    Join Date
    Sep 2002
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I figured it out, the variable i was passing to the function was not in quotes, it works great now!

    Thank you all so much for you help

    - Dave


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
  •