Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Automatically set focus on Text Field- JavaScript?

Posted on 2011-02-22
8
Medium Priority
?
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

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 1000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

662 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