473,397 Members | 1,961 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,397 software developers and data experts.

giving focus to an html element

Claus Mygind
571 512MB
I am creating an element dynamically as I load it on the screen. When it was hard coded on the page I had no problem executing the following two lines.

document.getElementById("SEARCH").select();
document.getElementById("SEARCH").focus();

Now I create the element the following way (see below) in my js function followed by the two lines above. I get no error message, but the input field has not received focus.


Expand|Select|Wrap|Line Numbers
  1. td.innerHTML =    '<INPUT'+
  2.     ' type="text"'+ 
  3.     ' name="SEARCH"'+ 
  4.     ' id="SEARCH"'+
  5.     ' maxLength='+ cMax + 
  6.     ' size='+ cLength +
  7.     ' onKeyPress="if (isEnter(event)) {'+cInsert+'};"'+
  8.     ' />';
May 28 '08 #1
4 1887
Claus Mygind
571 512MB
It appears to be a timing issue.

I am using FireFox v 2.0.0.14

I just moved the two lines into their own function and invoked the setTimeout() method and now it works fine like this:

Expand|Select|Wrap|Line Numbers
  1. var TimeoutID = setTimeout("giveSearchFocus()", 50);
  2.  
  3. function giveSearchFocus(){
  4.     document.getElementById("SEARCH").select();
  5.     document.getElementById("SEARCH").focus();
  6. }
May 28 '08 #2
acoder
16,027 Expert Mod 8TB
Thanks for posting. Yes, that makes sense. It would take a bit of time (even a few milliseconds) for the element to appear in the DOM.

PS. please use [code] tags when posting code. Thanks!
May 28 '08 #3
gits
5,390 Expert Mod 4TB
It appears to be a timing issue.

I am using FireFox v 2.0.0.14

I just moved the two lines into their own function and invoked the setTimeout() method and now it works fine like this:

Expand|Select|Wrap|Line Numbers
  1. var TimeoutID = setTimeout("giveSearchFocus()", 50);
  2.  
  3. function giveSearchFocus(){
  4.     document.getElementById("SEARCH").select();
  5.     document.getElementById("SEARCH").focus();
  6. }
hi ...

just another note since there is a possibility to avoid the eval within the timeout ... just use the function-reference instead of the 'eval-like' parameter -> the following would be sufficent and avoids the eval-operation:

Expand|Select|Wrap|Line Numbers
  1. function giveSearchFocus() {
  2.     document.getElementById("SEARCH").select();
  3.     document.getElementById("SEARCH").focus();
  4. }
  5.  
  6. var TimeoutID = setTimeout(giveSearchFocus, 50);
  7.  
kind regards
May 28 '08 #4
Claus Mygind
571 512MB
Thanks for posting. Yes, that makes sense. It would take a bit of time (even a few milliseconds) for the element to appear in the DOM.

PS. please use code tags when posting code. Thanks!
Sorry for the improper use of code tags not sure how they work, but in the future I will attempt to do that.

Expand|Select|Wrap|Line Numbers
  1. some code
this is just my test of using code tags no response necessary.
May 29 '08 #5

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

Similar topics

4
by: CharlesTheHawk | last post by:
I thought this would be really simple, but i'm stumped. I'd like to validate some data without submitting the form, but i can't seem to get the focus to go back to the field with the bad data. I've...
5
by: Stefano | last post by:
Hi, i want set focus element with element.focus() but i have always this exception: "focus() is not a function" Can you help me? Thanks I use Mozilla
20
by: Arne | last post by:
During testing <div style="overflow:auto;"> in CSS I noticed the mousewheel would work in Mozilla only after I made a <a href="#">some text</a> link and clicked on that, within the div. It...
2
by: Peter Wright | last post by:
Hi all. Hopefully this should demonstrate the problem I'm having: http://flooble.net/~pete/focus-problem-demo/ (I'm testing it in Mozilla only, but I'm not sure if it's actually a...
3
by: Praveen | last post by:
In IE a table element will receive focus when you either tab into it or when you click anywhere within the table. Mainly it fires the onfocus event. This doesn't happen in Mozilla (Firefox and...
3
by: VA | last post by:
t=document.getElementById('mytable') is a HTML table with some input fields in its cells Why doesnt t.getElementsByTagName('tr').firstChild.focus; put the focus on that text field? It...
9
by: fidodido | last post by:
If in the textarea (textarea3), the value is not "abc", and the user uses "Tab" to go to the next textarea (textarea4), it will alert an error message...and the focus will return to the textarea...
2
dlite922
by: dlite922 | last post by:
Before traversing my code, here's what my goal is and what this function does: I have a table of fields that dynamically grows as the user enters information. A minimum of 3 rows must always...
2
by: Jonathan N. Little | last post by:
As part of a JavaScript precheck form validation I noticed a problem with trying to return focus to the field with an error. I have setup a demo page. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.