Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

Automatically set focus on Text Field- JavaScript?

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
mikegeorge2
Asked:
mikegeorge2
1 Solution
 
Chad HaneyChief Technology OfficerCommented:
try this in the on load
document.getElementById("input_1_1").focus();

Open in new window

0
 
jmnfCommented:
change your body onload for this:

onload="document.getElementById('input_1_1').focus();"
0
 
HonorGodCommented:
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
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
mikegeorge2Author Commented:
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
 
mikegeorge2Author Commented:
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
 
mikegeorge2Author Commented:
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
 
jmnfCommented:
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
 
mikegeorge2Author Commented:
Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now