473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Onload focus to form element w/o using <body> tag.

Using Javascript, how do I give focus to a form text box on page load without using the <body> tag?

Thanks!
Jun 21 '07 #1
9 7154
pbmods
5,821 Expert 4TB
Heya, tomcadman. Welcome to TSDN!

You can call the element's focus() method anytime after you define it in the script.
E.g., this works:

Expand|Select|Wrap|Line Numbers
  1. <input id="theInput" type="text" value="" />
  2. <script type="text/javascript">
  3. document.getElementById('theInput').focus();
  4. </script>
  5.  
But this won't:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. document.getElementById('theInput').focus();
  3. </script>
  4. <input id="theInput" type="text" value="" />
  5.  
Jun 22 '07 #2
shoonya
161 100+
make a function
call it on
Expand|Select|Wrap|Line Numbers
  1. onload
event
Expand|Select|Wrap|Line Numbers
  1. document.getElementById(text_field_name).focus()
or

Expand|Select|Wrap|Line Numbers
  1. document.form_name.text_field_name.focus()
shoonya
Jun 22 '07 #3
The plot thickens... what is the chance I an use such code on page that is dynamically generated?

Details: Client hired an e-cart, wants changes. But limited access to code- i.e. via code templates. I can code one header for all pages, one footer, one body tag, etc. and I change the in-between code for specific pages.

The page content in question is dynamically repeated for the number of products the client has to list (i.e. I make one block of code to present all products).

See next page after this link http://www.king-cart.com/TVtest/frontpage=yes by pressing "Federal Standard Colors Products" button. Then view source.

The specific text box (labled 'COLOR-CODE') to be refocused with every load is in the 4th product down the list and it too is dynamically generated. I know its name from doing a 'view source' on the generated page- i.e "option|Color-Code|4".

Q: How can I focus on this text box? Possible?

Thanks for any suggestions!

-Tomcadman
Jun 22 '07 #4
pbmods
5,821 Expert 4TB
Heya, Tomcadman.

It sounds like the easiest solution will be to hand-code the focus() call into each page. Not all pages are going to have a form, and the element you want to have focused is likely to change depending on the page.
Jun 22 '07 #5
If you view source at page after link as ref'd above, you can see where I attempted to do this but no go. (ref. lines 371 and 381).

371 is where text box is generated. 381 is where I put code to focus.

What am I missing?

Ever-learning,
-tomcadman
Jun 22 '07 #6
pbmods
5,821 Expert 4TB
Heya, Tomcadman.

Alrightey. Now that I've actually looked at the source code (:P), here's some goodies:
  • Unrelated, but in your scripts1.js file, you have <script> tags, which are unnecessary (and in fact generate errors) in .js files.
  • You have no element whose id is 'option|Color-Code|4'. You have one element whose name is 'option|Color-Code|4', but that's not the same thing. You'd need to specifically set the element's ID. If you absolutely have no choice and must use name instead of ID, try calling this instead:

Expand|Select|Wrap|Line Numbers
  1. document.getElementsByName('option|Color-Code|4')[0].focus();
  2.  
  • Your code is attempting to call the 'option|Color-Code|4' focuser once for every product on the page. This is bad because there are situations where there is no element with ID/name 'option|Color-Code|4' when some of those statements are executed, and in some browsers, this means that your JavaScript will stop getting executed.

Ok, so it's a template; this is understandable. If it's possible to put this code somewhere else in your content so that it goes only on that page, but doesn't end up in the product listings, this would be preferable. Otherwise, you'll need to change your code slightly to only call focus() if the element exists:

Expand|Select|Wrap|Line Numbers
  1. if((! elementFocused) && (elementFocused = document.getElementById('option|Color-Code|4')))
  2.     elementFocused.focus();
  3.  
  4. //    Or...
  5. if((! elementFocused) &&    (elementFocused = document.getElementsByName('option|Color-Code|4')) && (elementFocused = elementFocused[0]))
  6.     elementFocused.focus();
  7.  
This code will check for a global variable named elementFocused. If it is unset, then the the script will attempt to find the element whose ID (or name, depending on which set you use) is 'option|Color-Code|4'.

If the element doesn't exist, elementFocused will be set to null, which evaluates to false, so we won't try to focus the element. And the next time we reach this set of statements, elementFocused will still be null (false), so we'll try to focus the element again.

If the element exists, we'll focus it, and then the next time we try to focus the element, elementFocused will be a reference to an element, which evaluates to true, so we'll skip trying to focus the element (since it's already been focused).

Note that in the second set, we have to additionally make sure that there are any elements named 'option|Color-Code|4' before we try to get the 0th one.
Jun 22 '07 #7
I tried using the simpler code (figuring I will add the 'if exists' code after I get simple to work.)

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. document.getElementsByName('option|Color-Code|4')[0].focus();
  3. </script>
Text box only gets focus once (on page load.)

Apparently, the cart's response (when "Add to Cart" is pressed) is not enough to trigger the focus again. I have to 'View Cart' or another page. Then when I return to products, the text box gets focus again.

Also, doesn't matter where I place it- i.e. in footer or inline on page.

So how do I create a trigger to focus on this text on page refresh (or whatever is happening after cart's code responds.)?

Any suggestions would be appreciated,

Thanks!

-tomcadman
Jun 23 '07 #8
Checking back in on this...

Anybody have a suggestion for above code challenge?

Thanks!
-tomcadman
Jul 3 '07 #9
kovik
1,044 Expert 1GB
Well, it will only occur when you tell it to. JavaScript is an event-driven language and can only respond to events, or be called when manually invoked. If you want it to focus, you have to tell it to focus. That's as simple as it gets.

If it won't focus, you're calling it incorrectly.
Jul 3 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Starry Gordon | last post by:
I've been running some small test programs which seem to indicate something noticed in a larger script, that a function called from onLoad() in the <body> tag will not succeed in creating a window...
1
by: Dung Ping | last post by:
For instance, one set is: <body onload="blinking_header()" onunload="stoptimer()"> Another set is: <body onload="writemsg()" onunload="stoptimer()"> They represent two functions. How to...
9
by: bmgz | last post by:
I need to execute a JavaScript function "onload". The only problem is I don not have access to the <body> tag as it is a part of the standard page-header include (a separate file). How could I have...
3
by: francescomoi | last post by:
Hi. I'm trying to insert some text between <head> and <body> but I'm not able. I try with: -------- bodyPoint = document.getElementsByTagName('body'); var myLink =...
2
mickey0
by: mickey0 | last post by:
HI, is it possible to do this? I'd like at first execute the ONE and then the TWO. Will it work? <body onload="func1()","func2()"> </body>
8
by: Prisoner at War | last post by:
Another weekend, another project, another problem.... Why should a perfectly fine function work only half-way through when called through onClick in an anchor tag?? The text fade-in script...
23
by: Xah | last post by:
Here's a interesting case of invalid html 4 strict file. In summary, if you have <body></bodywithout any content, the file would be invalid under html 4 strict. Valid if html 4 lose. <!DOCTYPE...
3
by: PYG | last post by:
Hi everybody I have a simple question : If i use this code : <body style="font-size:24px;color:blue;"> Text in body <table> <tr><td> Text in table
8
by: flydev | last post by:
Hello, Thanks for taking the time to help. I'm having a problem with GMap and IE7 (could be occurring in earlier versions). First let me give you a link to the problem: problem The problem...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.