Isha Goyal
Isha Goyal

Reputation: 105

focus is not working on textarea

I am creating a page in which a user can add a question and its solution, he can delete the problem and can also edit it dynamically using DOM in javascript. I want that whenever user clicks on edit button the textbox which appears get autofocus.

This the javascript code of my page...

var questionText;
var answerText;
var questionArray=[];
var answerArray=[];
var i=0;
var j=10000;

function addProblem(){
    var body = document.getElementsByTagName('body')[0];
    questionText = document.getElementById('questionId').value;
    answerText = document.getElementById('answerId').value;

    questionArray.unshift(questionText);
    answerArray.unshift(answerText);

    var myContainer = document.getElementById('container');
    var myDiv = document.createElement("div");
    var questionLogo = document.createElement("p");
    questionLogo.id = "questionLogo";
    var textNode = document.createTextNode("Question:");
    var question = document.createElement("p");
    question.id = "question";
    var questionDetail = document.createTextNode(questionArray[0]);

    var deleteButton = document.createElement("button");
    deleteButton.innerHTML = "Delete";
    deleteButton.id = i;
    var editButton = document.createElement("button");
    editButton.innerHTML = "Edit";
    editButton.id = j;

    var answerLogo = document.createElement("p");
    answerLogo.id = "answerLogo"
    var ansTextNode = document.createTextNode("Answer: ");
    var answer = document.createElement("p");
    answer.id = "answer";
    var answerDetail = document.createTextNode(answerArray[0]);
    var mybr = document.createElement("br");

    if(i==0){
        myContainer.appendChild(myDiv);
        myDiv.appendChild(questionLogo);
        questionLogo.appendChild(textNode);
        questionLogo.appendChild(question);
        question.appendChild(questionDetail);
        myDiv.appendChild(answerLogo);
        answerLogo.appendChild(ansTextNode);
        answerLogo.appendChild(answer);
        answer.appendChild(answerDetail);
        answerLogo.appendChild(mybr);
        myDiv.appendChild(deleteButton);
        myDiv.innerHTML += ' ';
        myDiv.appendChild(editButton);
    }
    else if (i > 0)
    {
        myContainer.insertBefore(myDiv,myContainer.firstChild);
        myDiv.appendChild(questionLogo);
        questionLogo.appendChild(textNode);
        questionLogo.appendChild(question);
        question.appendChild(questionDetail);
        myDiv.appendChild(answerLogo);
        answerLogo.appendChild(ansTextNode);
        answerLogo.appendChild(answer);
        answer.appendChild(answerDetail);
        answer.appendChild(mybr);
        myDiv.appendChild(deleteButton);
        myDiv.innerHTML += ' ';
        myDiv.appendChild(editButton);
    }

    i++;
    j++;

    myDiv.childNodes[7].addEventListener("click", function(){
        var deleteElement = document.getElementById(this.id);
        deleteElement.parentNode.parentNode.removeChild(deleteElement.parentNode);
    });

    myDiv.childNodes[9].addEventListener("click",function(){
         var editElement = document.getElementById(this.id);
         var quesEdit = editElement.parentNode.childNodes[1];
         var quesEditText = quesEdit.innerHTML;

         var ansEdit = editElement.parentNode.childNodes[4];
         var ansEditText = ansEdit.innerHTML;

         var editDiv1 = document.createElement("div");
         editDiv1.id = "editDiv1"
         var quesTextArea = document.createElement("textarea");
         quesTextArea.innerHTML += quesEditText;
         quesTextArea.focus();
         var saveButton1 = document.createElement("button");
         saveButton1.innerHTML = "Save";
         editDiv1.appendChild(quesTextArea);
         editDiv1.innerHTML += ' ';
         editDiv1.appendChild(saveButton1);
         quesEdit.parentNode.replaceChild(editDiv1,quesEdit);

         var editDiv2 = document.createElement("div");
         editDiv2.id = "editDiv2"
         var ansTextArea = document.createElement("textarea");
         ansTextArea.innerHTML += ansEditText;
         var saveButton2 = document.createElement("button");
         saveButton2.innerHTML = "Save";
         editDiv2.appendChild(ansTextArea);
         editDiv2.innerHTML += ' ';
         editDiv2.appendChild(saveButton2);
         ansEdit.parentNode.replaceChild(editDiv2,ansEdit);
    });
}

I have tried to focus the textarea using quesTextArea.focus(); but its not working where questextArea is the name of the textarea. Please help how i can do it.

Upvotes: 4

Views: 12398

Answers (5)

Sekar
Sekar

Reputation: 387

var div = document.getElementById('parent');
var txt = document.createElement('textarea');
div.appendChild(txt);
txt.focus();
<html>

<head></head>

<body>
  <div id="parent">
    <input type="text" value="" />
  </div>
</body>

</html>

The element must be in the DOM when you invoke the focus function. Move your focus() function after the appendChild() is invoked.

 quesTextArea.innerHTML += quesEditText;
 var saveButton1=document.createElement("button");
 saveButton1.innerHTML="Save";
 editDiv1.appendChild(quesTextArea);
 quesTextArea.focus();

Upvotes: 0

Trung Duong
Trung Duong

Reputation: 3475

For the element could be got focused, it must be in the DOM when you invoke focus on it. You should invoke focus function after replaceChild function

 editDiv1.appendChild(quesTextArea);
 editDiv1.appendChild(saveButton1);
 quesEdit.parentNode.replaceChild(editDiv1,quesEdit);
 quesTextArea.focus();

I've created a simple sample as below link, you could check it
https://jsfiddle.net/pd9c6c7a/3/

Upvotes: 5

uautkarsh
uautkarsh

Reputation: 502

Try the following approach:

var body=document.getElementsByTagName('body')[0];
var quesTextArea=document.createElement("textarea");
var button=document.createElement("button");
button.innerHTML = "click Me";
button.addEventListener("click",function(e){
  e.preventDefault();
  quesTextArea.focus();
});
body.appendChild(quesTextArea);
body.appendChild(button);
<html>
<body>
<body>
</html>

Try to add preventDefault.

Upvotes: 0

Mac A.
Mac A.

Reputation: 103

The 'textarea' has not been added to window to be shown, an element must be part of the document object tree. In case that didn't work, add a 50ms delay.

setTimeout(function(){e.focus();}, 50);

Upvotes: 0

brightDot
brightDot

Reputation: 339

Add autofocus attribute to the textarea element. So that whenever it is appended to the DOM, will get cursor activated in it.

Upvotes: 3

Related Questions