Welcome to WebmasterWorld Guest from 54.146.176.35

Forum Moderators: open

Message Too Old, No Replies

Setting focus

without using body onload

     
9:35 pm on Dec 25, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 28, 2001
posts:779
votes: 0


Hey all,

I'm trying to set focus on a form field but I'm using a cms where I can't edit the body tag or edit the head section.

Well, I can, but it's only on one page I want to use the code (the login page) and so I don't need the script on every page.

So here's the thing.

I've tried posting:
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!-- self.focus();document.login.username.focus() //--> </SCRIPT>

before and after the form (login) and it doesn't work.

Any tips on how to set the focus without changing the body tag or adding the JS to the head?

9:58 pm on Dec 25, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 30, 2004
posts:712
votes: 0


<form> ... </form>

<script type="text/javascript">
document.getElementById('loginform').getElementsByTagName('input').item(0).focus();
</script>

Looks for an element with an id of "loginform"
Looks for <input> tags
Looks for the specified item. The first input tag is 0, the second is 1, etc
And then sets the focus..

10:07 pm on Dec 25, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Sept 28, 2001
posts:779
votes: 0


Cool!

But here's a problem - It seems to work initially when that form is loaded - But as the rest of the page loads, it looses focus.

Suggestions? Maybe I'll try placing the JS somewhere else..

10:47 pm on Dec 25, 2006 (gmt 0)

New User

5+ Year Member

joined:Dec 13, 2006
posts:28
votes: 0


Try putting the javascript at the end of the HTML, right before/after the body tag.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members