By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,961 Members | 1,803 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,961 IT Pros & Developers. It's quick & easy.

change focus using variable

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.