Solved

Automatically set focus on Text Field- JavaScript?

Posted on 2011-02-22
8
416 Views
Last Modified: 2012-05-11
I know this is normally a simple question, but my situation is a little different. (of course)

I am using a plug-in that helps me create forms with conditional logic in Wordpress. You can see the form at a temporary URL, which is http://highlifegourmet.com

This is important to know, because I don't have any access to the form code itself. I am using a plug-in that creates the code. I can style it using CSS but that's it.

There is a transparent form field just to the left of the words "enter zip code"

I need to bring focus to it, so people can see that it's actually a form. (or maybe somebody has a better idea?)

I tried this in my body:

<body OnLoad="document.gform_1.gform_fields_1.focus();">

but it did not work because the script is targeting the form via the name attribute. The plug-in doesn't give the form a name attribute so it doesn't know what to do.

I need the code for a script where I can use the form ID instead of the name attribute. Someone also told me I could "always use jQuery to dynamically set a name attribute on the form when the page loads."

I am just a mortgage broker and I don't know what any of this means. I don't know what JQuery is...

I guess I'm just looking for a code snippet.

Thank you very much. I appreciate it.
0
Comment
Question by:mikegeorge2
8 Comments
 
LVL 12

Expert Comment

by:Chad Haney
ID: 34954242
try this in the on load
document.getElementById("input_1_1").focus();

Open in new window

0
 
LVL 4

Expert Comment

by:jmnf
ID: 34954354
change your body onload for this:

onload="document.getElementById('input_1_1').focus();"
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 34954398
I have seen / experienced a problem with trying to "put focus" on a field.
The way around it?  I needed to have a little tiny delay between the getElementById() and
using that reference to call / invoke the focus() method.

For example, something like:
//------------------------------------------------------------------
  // Name: focusOn()
  // Role: Provide a way to delay the field.focus() request
  // Note: The Gecko engine (Mozilla, Firefox, et.al.) may throw an
  //       uncatchable excption "Permission denied".  This has
  //       supposedly been fixed in Firefox 1.5.1
  //------------------------------------------------------------------
  function focusOn( id ) {
    var field = document.getElementById( id );
    if ( field ) {
      field.focus();    // May throw "Permission denied" (uncatchable)
    } else {
      alert( 'unknown section id "' + id + '"' );
    }
  }

...

setTimeout('focusOn("src")',10 );

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:mikegeorge2
ID: 34954424
I tried, but it didn't seem to work. Is there a special place that code should be? Right now, I have it in the body.
0
 

Author Comment

by:mikegeorge2
ID: 34954547
Sorry. I was posting at the same time you guys were. I haven't tried the latest solution (from HonorGod). I'll be right back!
0
 

Author Comment

by:mikegeorge2
ID: 34954629
HonorGod, I am sorry, but I am not technically savvy enough to fully understand your post.

Should I just cut and paste your code? Where do I put it in relation to the body onload onload="document.getElementById('input_1_1').focus();" ?

Thank you so much.

0
 
LVL 4

Accepted Solution

by:
jmnf earned 250 total points
ID: 34954683
do this on the code, after the form </form>, so the form loads first, and then the script that sets focus loads... if that does not work, put it right before the body closes </body>

<script languaje="javascript">
    document.getElementById('input_1_1').focus();
</script>

Open in new window

0
 

Author Closing Comment

by:mikegeorge2
ID: 34954810
Thank you!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing CSS, there are a few simple rules that will make your life easier.    1. Using ‘* {box-sizing:border-box;}’. Using this will wrap all your elements in a nice little compact box-model that will give you the width you want, like so... …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
In this Micro Tutorial viewers will learn how to create a CSS image sprite (In a later tutorial, viewers will learn how to use CSS and HTML to create a navigation menu using this sprite) Open a new Photoshop document with a width of (Icon width)x(N…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question