I am adding a search feature to a page, but that is not the primary purpose of the page. I have a "Search" link that jumps to an anchor tag, but I would also like for the cursor to be placed in the search field when this link is clicked. Searches of the forums and Google have revealed little, although that may be because I'm not exactly sure what to search for on this matter. Help is appreciated. TIA.
Move cursor to text input?
Just to make sure I understand the desired behavior:
You have a page with a link. When you click on the link, it goes to the same page but to an anchor tag, and at this point you want the cursor to be in an input box?
You can do it with javascript. PHP doesn't interact with the client, it only runs on the server. javascript runs on the client.
jscode:
function makeselection(inputID)
{
document.getElementById(inputID).focus();
}
something like that should do the trick. along with the right HTML:
<a href="page.html#anchor" onClick="makeselection(searchInput);">Search</a>
Something like that.
Jeremuck-
Yes, that is the desired affect I am looking for. I have tried the code you suggested (with some slight mods). However, I am unfamiliar with javscript, so it doesn't seem to work correctly. Here is what I have:
<script = "text/javascript">
<!--
function searchfocus()
{ document.getElementById(frm_search.term).focus(); }
-->
</script>
<a href="#findorder" onclick="javascript:searchfocus();">Search</a>
...
<form name="frm_search" action="search.php" method=GET>
<input type=text name=term>
<input type=submit value="Find">
</form>
This code produces a javascript error when I click the link saying that "document.getElementByID(...) is null or not an object".
The field you're looking for has to have an id attribute, and getElementById would use that to get it into Javascript (hence "get Element By Id").
More informative help might be available in a Javascript forum.
It's close.
<script = "text/javascript">
<!--
function searchfocus()
{
document.frm_search.term.focus();
}
-->
</script>
<a href="#findorder" onclick="searchfocus();">Search</a>
...
<form name="frm_search" action="search.php" method=GET>
<input type=text name=term>
<input type=submit value="Find">
</form>
That should "cut the mustard"-- hehe what a funny phrase. The way using getElementbyId(IDname) is if you have many different forms on the page, and you ant to dynamically choose different forms to take the input focus. Also note, name and id are different. So when you using getElementbyID, you need the ID, not the name.
<input type=text name=term id=term>
THen you could use getElementbyId("term");
Anyway, it shoudl work the way I posted above.