Jump to content
MakeWebGames

Javascript - Focus on input field on page load


AlabamaHit

Recommended Posts

First off, I in 'no' way take credit for writing this script. I just found it on 'google'. (Sorry, I can't remeber where.).

What I did was took the javascript and made a super simple function out of it, to save typing so you can use it on all your pages you want. Sorry, I don't have an example of this, I don't have hosting for my engine (yet). So I have nothing online to show this. I made this for my engine. But it's just a function so it will work on anything..

Here is the function (Don't talk bad about it. It's VERY simple, not complex. I Know that there are lots of ways of doing this.).

 

function focus'$form,$input)
{
echo '<script type="text/javascript" language="JavaScript">';
echo "document.forms['".$form."'].elements['".$input."'].focus();";
echo '</script>';
}

 

Now it used in action. (Just an example so please don't pick at it. or try to talk bad about it.

 

echo '
<form action="PAGENAME" method="post" name="form_name_here">
<input type="text" name="input_name" value="Nothing" />
<input type="submit" value="Do It" />
</form>';
//Now we use the function.
focus('form_name_here','input_name');
//Yes it MUST be AFTER the FROM! <-- Very important or it will not work.
//First name is forms name. <-- In example it is (form_name_here)
//Second is the Inputs name that you want it to put the cursor on when loaded. <-- In example it is (input_name)
//Now, when they load the page it will put the cursor in that input everytime :)

 

Hope you enjoy, yeah I know this is really nothing special. But I'm sure there are lots of people like me that are lazy :P. I use stuff like this all the time to save typing.

I'm sure everyone is going to say how they would have done it, lol..

But that is ok :thumbsup:

Link to comment
Share on other sites

Yeah on the engine I'm working on, I'm making the page focus on the first input on any page that has an input..

So, if using mccodes, I would agree completely, pages like login, register, bank, home page(index) focus on notepad, etc...

In mccodes I would put it in the global function page, But if using on login, you will have to put it on the page. So really wouldn't need the functnion. Just adding the javascript itself to pages like that.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...