Variable problem with multi-use function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Art X

    Variable problem with multi-use function

    The reason for this has do to with the nature of WebTV and will be
    directed to WebTV users with a conditional so that it will have no
    effect on PC users.

    I'm trying to create a function that will put focus to a form element
    (with a short delay) when the division that it is in is changed from
    hidden to visible.

    For a one time use, this works:

    function showDiv()
    {
    if (navigator.appN ame.indexOf("We bTV") != -1)
    {
    setTimeout("doc ument.getElemen tById('theinput ').focus();", 50);
    }
    document.getEle mentById("hidde ndiv").style.vi sibility = "visible";
    document.getEle mentById("visib lediv").style.v isibility = "hidden";
    }

    Both divisions have these embedded CSS rules:

    #visiblediv, #hiddendiv {
    width: 100%;
    height: 100%;
    text-align: center;
    position: absolute;
    left: auto;
    top: 65px;
    }
    #visiblediv {
    visibility: visible;
    }
    #hiddendiv {
    visibility: hidden;
    }

    And in the body:

    <a href="javascrip t:void();" onclick="openDi v(); return false;">Display
    The Form</a>

    <div id="visiblediv" >
    division content
    </div>

    <div id="hiddendiv" >
    division content including a text link with a function to hide this
    division again and a form containing the text input with the id of
    "theinput"
    </div>

    However, I want to use this function more than once. When I try this it
    doesn't work:

    function showDiv(in_foc)
    {
    if (navigator.appN ame.indexOf("We bTV") != -1)
    {
    setTimeout("doc ument.getElemen tById(in_foc).f ocus();", 50);
    }
    document.getEle mentById("hidde ndiv").style.vi sibility = "visible";
    document.getEle mentById("visib lediv").style.v isibility = "hidden";
    }

    Then in the body:

    <a href="javascrip t:void();" onclick="openDi v('theinput'); return
    false;">Display The Form</a>

    hiddendiv becomes visible but the focus() does not work. I realize it
    has to do with how I'm addressing the variable, I just can't figure out
    what I'm doing wrong.

    Later, Art.

  • Jc

    #2
    Re: Variable problem with multi-use function

    Art X wrote:[color=blue]
    > function showDiv(in_foc)
    > {
    > if (navigator.appN ame.indexOf("We bTV") != -1)
    > {
    > setTimeout("doc ument.getElemen tById(in_foc).f ocus();", 50);
    > }
    > document.getEle mentById("hidde ndiv").style.vi sibility = "visible";
    > document.getEle mentById("visib lediv").style.v isibility = "hidden";
    > }
    >
    > Then in the body:
    >
    > <a href="javascrip t:void();" onclick="openDi v('theinput'); return
    > false;">Display The Form</a>
    >
    > hiddendiv becomes visible but the focus() does not work. I realize it
    > has to do with how I'm addressing the variable, I just can't figure out
    > what I'm doing wrong.[/color]

    I scanned your code and didn't see anything obvious, I don't see a
    problem with the way you're passing a string into your function. The
    only problem I see is that you are calling openDiv instead of showDiv,
    is that the problem or is that a result of the function being later
    renamed?

    When I write code to show or hide things, I usually make the function a
    toggle. For example, I would call it toggleDivDispla y, and pass in the
    desired div id's. This makes the code reusable, and you don't have to
    write a second function to hide the div.

    Comment

    • RobG

      #3
      Re: Variable problem with multi-use function

      Art X wrote:[color=blue]
      > The reason for this has do to with the nature of WebTV and will be
      > directed to WebTV users with a conditional so that it will have no
      > effect on PC users.
      >
      > I'm trying to create a function that will put focus to a form element
      > (with a short delay) when the division that it is in is changed from
      > hidden to visible.
      >[/color]

      I don't have WebTV to test this on, but here goes anyway (it works in
      IE and Firefox at least :-D ).

      [...][color=blue]
      > if (navigator.appN ame.indexOf("We bTV") != -1)
      > {
      > setTimeout("doc ument.getElemen tById(in_foc).f ocus();", 50);[/color]

      setTimeout wants a string as an argument. in_foc is evaluated when
      setTimeout runs, but it is a local variable inside showDiv() and so
      returns 'undefined' and the focus method fails (or likely is never
      called since document.getEle mentById fails).

      Use this:

      setTimeout("doc ument.getElemen tById('" + in_foc + "').focus() ;", 50);

      in_foc will be evaluated when the call to setTimeout is made and its
      value inserted (note the extra quotes).

      [...]

      --
      Rob

      Comment

      • Art X

        #4
        Re: Variable problem with multi-use function

        Jc,

        That is a mistake on my part when I made the post. The function call in
        the link should have been the same as the function name. Which is how it
        is on the page that I'm using it on. I apologize for the confusion that
        may have caused. Your suggestion about the toggle function is
        outstanding and something I'll start using immediately.

        Rob,

        I tried that concatenation of few days ago and couldn't figure out why
        it didn't work. As soon as I saw your example I realized my error, I
        wasn't including the single quotes. Another "duh me", which is the
        source of most of my frustrations. I just tried what you posted and it
        works like a charm.

        Thank you very much Jc and Rob for your replies and help.

        Later, Art.

        Comment

        Working...