470,591 Members | 2,208 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

change focus using variable

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/
Jul 23 '05 #1
6 2386
Brian wrote:

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?

document.forms[category].q.focus()

But why the anchors?

Mick
Jul 23 '05 #2
In article <10*************@corp.supernews.com>, usenet3
@julietremblay.com.invalid enlightened us with...
I'm trying unsuccessfully to use a variable in a script that changes focus.

I will assume these are form elements and the form is named, say, form1.

The script I have is:

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


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

Jul 23 '05 #3
Mick White wrote:
document.forms[category].q.focus()
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 >
why the anchors?


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/
Jul 23 '05 #4
kaeli wrote:
us*****@julietremblay.com.invalid enlightened us with...
I'm trying unsuccessfully to use a variable in a script that changes focus.


I will assume these are form elements and the form is named, say, form1.


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>
The script I have is:

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


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


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/
Jul 23 '05 #5
On Fri, 07 May 2004 16:43:56 -0400, Brian
<us*****@julietremblay.com.invalid> wrote:
Mick White wrote:
document.forms[category].q.focus()


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>


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.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #6
Michael Winter wrote:
Brian wrote:
function changeFocus(category) {
document.forms[category].q.focus();
return false;
}

<A onClick="changeFocus('basicsearch')"
HREF="#basic">basic site search</A>
You need to cancel the navigation for the focus to take effect (it
also acts very strangely on Mozilla, otherwise).


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


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/
Jul 23 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by fish | last post: by
4 posts views Thread by Paul Thompson | last post: by
4 posts views Thread by Richard Cornford | last post: by
3 posts views Thread by Philip Townsend | last post: by
4 posts views Thread by Jon Slaughter | last post: by
3 posts views Thread by Simon Tamman | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.