Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Variable problem with multi-use function

Reply
Thread Tools

Variable problem with multi-use function

 
 
Art X
Guest
Posts: n/a
 
      06-13-2005
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.appName.indexOf("WebTV") != -1)
{
setTimeout("document.getElementById('theinput').fo cus();", 50);
}
document.getElementById("hiddendiv").style.visibil ity = "visible";
document.getElementById("visiblediv").style.visibi lity = "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="javascript:void();" onclick="openDiv(); 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.appName.indexOf("WebTV") != -1)
{
setTimeout("document.getElementById(in_foc).focus( );", 50);
}
document.getElementById("hiddendiv").style.visibil ity = "visible";
document.getElementById("visiblediv").style.visibi lity = "hidden";
}

Then in the body:

<a href="javascript:void();" onclick="openDiv('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.

 
Reply With Quote
 
 
 
 
Jc
Guest
Posts: n/a
 
      06-14-2005
Art X wrote:
> function showDiv(in_foc)
> {
> if (navigator.appName.indexOf("WebTV") != -1)
> {
> setTimeout("document.getElementById(in_foc).focus( );", 50);
> }
> document.getElementById("hiddendiv").style.visibil ity = "visible";
> document.getElementById("visiblediv").style.visibi lity = "hidden";
> }
>
> Then in the body:
>
> <a href="javascript:void();" onclick="openDiv('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.


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 toggleDivDisplay, 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.

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      06-14-2005
Art X wrote:
> 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.
>


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

[...]
> if (navigator.appName.indexOf("WebTV") != -1)
> {
> setTimeout("document.getElementById(in_foc).focus( );", 50);


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.getElementById fails).

Use this:

setTimeout("document.getElementById('" + 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
 
Reply With Quote
 
Art X
Guest
Posts: n/a
 
      06-14-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Variable argument function as a parameter of a variable argument function AikidoGuy C Programming 11 11-21-2011 10:43 PM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
How to pass variable argument list to another function w/ variable argument list? Ben Kial C Programming 1 11-15-2004 01:51 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57