Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7740885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:53:27+00:00 2026-06-01T08:53:27+00:00

I have problems setting the focus on an input element that has been created

  • 0

I have problems setting the focus on an input element that has been created on the fly and which has had and lost focus previously. I’ve distilled it to this simple code:

I expect the focus to ping-pong between the two input elements as you type, but on Firefox and Chrome the focus stays in the first text box after the second has been created, received focus, and sent focus back to the first. Why is this?

<html>
<head>
<script type="text/javascript">
<!--
function onkey(event) {
    if(event.target.id == "b") {
        var c = document.getElementById("c");
        if(!c) {
            document.getElementById("a").innerHTML += "<br/><input id=\"c\" type=\"text\"/>";
            c = document.getElementById("c");
            document.getElementById("status").textContent = "created c "+c
        } else {
            document.getElementById("status").textContent = "activing c "+c;
        }
        c.onkeydown = onkey;
        c.focus();
    } else {
        document.getElementById("status").textContent = "activing b";
        document.getElementById("b").focus();
    }
}

function test() {
    var b = document.getElementById("b");
    b.onkeydown = onkey;
    b.focus();
}
//-->
</script>
<body onload="test();">
<noscript>
Sorry, you need javascript.  Not much to see here otherwise; move along.
</noscript>
<div id="status"></div>
<div id="a">
<input id="b" type="text"/>
</div>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:53:28+00:00Added an answer on June 1, 2026 at 8:53 am

    First of all, you should use jQuery.

    You are recreating the input field b when you add field c by using the += operator together with innerHTML, effectively destroying the event you previously created on field b.

    The code below will fix your problem but you should definitely use jQuery for this.

    <html>
    <head>
    <script type="text/javascript">
    <!--
    function onkey(event) {
        console.log(event.target.id);
        if(event.target.id == "b") {
            var c = document.getElementById("c");
            if(!c) {
                // here you reset all html within the a tag, destroying ALL events
                document.getElementById("a").innerHTML += "<br/><input id=\"c\" type=\"text\"/>";
                c = document.getElementById("c");
                // rebinding the event to b will fix the issue 
                document.getElementById("b").onkeydown = onkey;
                document.getElementById("status").textContent = "created c ";
            } else {
                document.getElementById("status").textContent = "activating c ";
            }
            c.onkeydown = onkey;
            c.focus();
        } else {
            document.getElementById("status").textContent = "activating b";
            document.getElementById("b").focus();
        }
    }
    
    function test() {
        var b = document.getElementById("b");
        b.onkeydown = onkey;
        b.focus();
    }
    //-->
    </script>
    <body onload="test();">
    <noscript>
    Sorry, you need javascript.  Not much to see here otherwise; move along.
    </noscript>
    <div id="status"></div>
    <div id="a">
    <input id="b" type="text"/>b
    </div>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data entry web app where the control that has focus is
I've got problems with setting focus to an input field after validating it. I've
I have setup some basic code that triggers on focus of various input fields:
Hallo. I have problems by setting the Eclipse envirorment, can anybody provide me a
I'm having problems settig up my application using Thin and Sinatra. I have created
Ever since deleting the original settings.settings file I have had endless problems getting the
I have a problem with setting the focus to the textbox for a login
I have an application written in .NET. The previous version had no problems: you
I usually don't have any problems setting up the classpath and running programs, but
I have problem with setting proper charset on my jsf pages. I use MySql

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.