2

So I am creating a new div using javascript and then I am trying to set the focus on the new div but it just isn't working. I have looked at this post Difficulty setting focus on newly created object in javascript but I still can't get it to work Here is my function:

function overlayDIV(clickedId) {

  // see if div already exists if not create it
  if (!document.getElementById("overlay" + clickedId)) {
    var div = document.createElement("div");
    div.id = "overlay" + clickedId;
    div.style.width = "12px";
    div.style.height = "12px";
    div.style.background = "red";

    div.style.position="absolute";
    div.style.zIndex = "1";

    //added this line after reading the other post, 
    div.setAttribute('tabindex', '0');

    document.getElementById(clickedId).appendChild(div);
  }

  document.getElementById("overlay" + clickedId).focus();
}

I have no idea what Im doing wrong if anyone can help that would be awesome thanks.

EDIT:
So I made a JSfiddle of it: http://jsfiddle.net/m7cQA/ Now what I want to be able to do is let the user click on the div and have a new div be created (this is happening already) and the user should be able to just start typing in the newly created div (this is the part that isn't working) when you start typing the text goes in the parent div behind the new div. You have to re-click on the divs to get the cursor to move to the new div in order to start typing in it.

Community
  • 1
  • 1
J_Derek
  • 56
  • 4
  • Can you give us a [JSFiddle](http://jsfiddle.net/) showing the issue? Just copy/paste your code. – Robbie Wxyz Apr 18 '14 at 22:39
  • 1
    I'm unclear on what happens if you have a div and give it focus. What are you trying to accomplish? – Surreal Dreams Apr 18 '14 at 22:47
  • possible duplicate of [Is it possible to focus on a
    using javascript focus() function?](http://stackoverflow.com/questions/3656467/is-it-possible-to-focus-on-a-div-using-javascript-focus-function)
    – Fraser Apr 18 '14 at 22:48

0 Answers0