I have a function for renaming certain divs. The way I'm trying to get it to work is like this:
I have most of the steps done but the input element is not being focused after I click 'rename'. Here's the code:
function Rename( ){
ClickedFile.innerHTML = "<input class='Rename' type='text' value='" + ClickedFile.innerHTML + "'>";
ClickedFile.childNodes[0].focus();
}
The ClickedFile is the node that was right clicked. Changing The innerHTML works fine but the .focus() does not and I'm not sure why. I don't get any errors on the console, either.
I've also tried using:
ClickedFile.childNodes[0].select();
ClickedFile.childNodes[1].focus();
ClickedFile.focus();
None of them have worked.
Edit:
I know using JQuery might help, but I'm more interested in finding out why this isn't working.
I fixed the problem. It has something to do with event handlers. My answer is posted below.
You have to add the element as part of DOM
var input = document.createElement("input");
input.className = "Rename";
input.type = "text";
document.getElementById("somenode").appendChild(input);
input.focus(); // should work now
see the fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With