Keroro Chan
Keroro Chan

Reputation: 99

javascript show cursor after foucs()

When I double click the text, the text box shows. And, I want to show the cursor at the end of text box. The problem is the cursor does not show but when I press a key the character is typed at the end of text box.

HTML:

<div ondblclick="change_to_textbox()" id="A">the original text</div>

Javascript:

function change_to_textbox(){
  var newtext = document.getElementById("A").innerHTML;
  document.getElementById("A").innerHTML = "<input type='text' id='B'>";
  document.getElementById("B").value = newtext;
  document.getElementById("B").focus();
}

How to show the cursor at the end of text box?

Upvotes: 0

Views: 48

Answers (5)

Vikas Rastogi
Vikas Rastogi

Reputation: 1

instead document.getElementById("B").focus;

write document.getElementById("B").focus();

.focus is a method so use with parentheses.

Upvotes: 0

Fernando de Bem
Fernando de Bem

Reputation: 116

You just forgot that focus is a function.

Try:

document.getElementById("B").focus();

Upvotes: 0

pradeep1991singh
pradeep1991singh

Reputation: 8365

Yes you missed focus()

function change_to_textbox(){
  var newtext = document.getElementById("A").innerHTML;
  document.getElementById("A").innerHTML = "<input type='text' id='B'>";
  document.getElementById("B").value = newtext;
  document.getElementById("B").focus();
}
<div ondblclick="change_to_textbox()" id="A">the original text</div>

Upvotes: 0

kweku360
kweku360

Reputation: 1075

try this

function change_to_textbox(){
  var newtext = document.getElementById("A").innerHTML;
  document.getElementById("A").innerHTML = "<input type='text' id='B'>";
  document.getElementById("B").value = newtext;
  document.getElementById("A").focus();
}

Hope it helps

Upvotes: 0

Sumanth Jois
Sumanth Jois

Reputation: 3234

You should change focus to focus()

 document.getElementById("B").focus;

to

  document.getElementById("B").focus();

Upvotes: 1

Related Questions