Connecting Tech Pros Worldwide Forums | Help | Site Map

change focus using variable

Brian
Guest
 
Posts: n/a
#1: Jul 23 '05
I'm trying unsuccessfully to use a variable in a script that changes focus.

Depending on which link a user follows (navigates via keyboard or
clicks), I'd like to change the focus to an input name="q" in different
forms on the page.

<A onClick="changeFocus('basicsearch')"
HREF="#basic">basic site search</A>

<A onClick="changeFocus('advancedsearch')"
HREF="#advanced">advanced site search</A>

The script I have is:

function changeFocus(category) {
document.'(category)'.q.focus();
return false;
}

It doesn't work. I'm not sure how to insert the parameter into the focus
statement. What am I doing wrong?

--
Brian (remove "invalid" from my address to email me)
http://www.tsmchughs.com/

Mick White
Guest
 
Posts: n/a
#2: Jul 23 '05

re: change focus using variable


Brian wrote:
[color=blue]
>
> The script I have is:
>
> function changeFocus(category) {
> document.'(category)'.q.focus();
> return false;
> }
>
> It doesn't work. I'm not sure how to insert the parameter into the focus
> statement. What am I doing wrong?
>[/color]
document.forms[category].q.focus()

But why the anchors?

Mick
kaeli
Guest
 
Posts: n/a
#3: Jul 23 '05

re: change focus using variable


In article <109npo6eeh8q8e2@corp.supernews.com>, usenet3
@julietremblay.com.invalid enlightened us with...[color=blue]
> I'm trying unsuccessfully to use a variable in a script that changes focus.
>[/color]

I will assume these are form elements and the form is named, say, form1.
[color=blue]
>
> The script I have is:
>
> function changeFocus(category) {
> document.'(category)'.q.focus();
> return false;
> }
>[/color]

document.forms['form1'].elements[category].focus();

--
--
~kaeli~
A lot of money is tainted - It taint yours and it taint mine.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Brian
Guest
 
Posts: n/a
#4: Jul 23 '05

re: change focus using variable


Mick White wrote:
[color=blue]
> document.forms[category].q.focus()[/color]

I'm now using the following script:

function changeFocus(category) {
document.forms[category].q.focus();
return false;
}

and the following html (edited for space):

<A onClick="changeFocus('basicsearch')"
HREF="#basic">basic site search</A>

<A onClick="changeFocus('advancedsearch')"
HREF="#advanced">advanced site search</A>

<H2 id="basic">basic site search</H2>

<FORM METHOD="get" ACTION="/foo/bar.pl" NAME="basicsearch">
<INPUT TYPE="text" NAME="q">
</FORM>

<H2 id="advanced">advanced site search</H2>
<FORM METHOD="get" ACTION="/foo/bar.pl" NAME="advancedsearch">
<INPUT TYPE="text" NAME="q">
</FORM>


Still no dice. Live test case here:

< http://www.tsmchughs.com/site/searchn >
[color=blue]
> why the anchors?[/color]

When .js is disabled, the bookmark anchor will move to an <h2> element
just above the form.

--
Brian (remove "invalid" from my address to email me)
http://www.tsmchughs.com/
Brian
Guest
 
Posts: n/a
#5: Jul 23 '05

re: change focus using variable


kaeli wrote:
[color=blue]
> usenet3@julietremblay.com.invalid enlightened us with...
>[color=green]
>>I'm trying unsuccessfully to use a variable in a script that changes focus.[/color]
>
> I will assume these are form elements and the form is named, say, form1.[/color]

It's the form element name that changes; the input that should get focus
is named "q" in each form, e.g.,

<FORM METHOD="get" ACTION="/foo/bar.pl" NAME="basicsearch">
<INPUT TYPE="text" NAME="q">
</FORM>
[color=blue][color=green]
>>The script I have is:
>>
>>function changeFocus(category) {
>> document.'(category)'.q.focus();
>> return false;
>>}[/color]
>
> document.forms['form1'].elements[category].focus();[/color]

I've tried

function changeFocus(category) {
document.forms[category].elements['q'].focus();
return false;
}

No luck.

--
Brian (remove "invalid" from my address to email me)
http://www.tsmchughs.com/
Michael Winter
Guest
 
Posts: n/a
#6: Jul 23 '05

re: change focus using variable


On Fri, 07 May 2004 16:43:56 -0400, Brian
<usenet3@julietremblay.com.invalid> wrote:
[color=blue]
> Mick White wrote:
>[color=green]
>> document.forms[category].q.focus()[/color]
>
> I'm now using the following script:
>
> function changeFocus(category) {
> document.forms[category].q.focus();
> return false;
> }
>
> and the following html (edited for space):
>
> <A onClick="changeFocus('basicsearch')"
> HREF="#basic">basic site search</A>
>
> <A onClick="changeFocus('advancedsearch')"
> HREF="#advanced">advanced site search</A>
>
> <H2 id="basic">basic site search</H2>
>
> <FORM METHOD="get" ACTION="/foo/bar.pl" NAME="basicsearch">
> <INPUT TYPE="text" NAME="q">
> </FORM>
>
> <H2 id="advanced">advanced site search</H2>
> <FORM METHOD="get" ACTION="/foo/bar.pl" NAME="advancedsearch">
> <INPUT TYPE="text" NAME="q">
> </FORM>[/color]

You need to cancel the navigation for the focus to take effect (it also
acts very strangely on Mozilla, otherwise). Prefix the onclick attribute
value with "return " so that the "return false" statement in the
changeFocus() function passes back to the intrinsic event.

Mike

--
Michael Winter
M.Winter@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Brian
Guest
 
Posts: n/a
#7: Jul 23 '05

re: change focus using variable


Michael Winter wrote:[color=blue]
> Brian wrote:
>[color=green]
>> function changeFocus(category) {
>> document.forms[category].q.focus();
>> return false;
>> }
>>
>> <A onClick="changeFocus('basicsearch')"
>> HREF="#basic">basic site search</A>[/color]
>
> You need to cancel the navigation for the focus to take effect (it
> also acts very strangely on Mozilla, otherwise).[/color]

That's what I was noticing, thought Opera seemed to act similarly.
[color=blue]
> Prefix the onclick attribute value with "return " so that the "return
> false" statement in the changeFocus() function passes back to the
> intrinsic event.[/color]

That's it! I didn't know I needed to add "return " to the onclick
statement as you described. Thanks for your help.

--
Brian (remove "invalid" from my address to email me)
http://www.tsmchughs.com/
Closed Thread