Reputation: 99
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
Reputation: 1
instead document.getElementById("B").focus;
write document.getElementById("B").focus();
.focus is a method so use with parentheses.
Upvotes: 0
Reputation: 116
You just forgot that focus is a function.
Try:
document.getElementById("B").focus();
Upvotes: 0
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
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
Reputation: 3234
You should change focus to focus()
document.getElementById("B").focus;
to
document.getElementById("B").focus();
Upvotes: 1