I have a function to rename specific divs. The way I'm trying to get it to work is as follows:
- User right click div
- User clicks "Rename" in the context menu
- Div gets an input element and automatically focuses
- The input comes after pressing the enter key or pressing the screen.
I have most of the steps, but the input element does not focus after clicking rename. Here is the code:
function Rename( ){ ClickedFile.innerHTML = "<input class='Rename' type='text' value='" + ClickedFile.innerHTML + "'>"; ClickedFile.childNodes[0].focus(); }
ClickedFile is a node that has been right-clicked. Changing innerHTML works fine, but .focus () does not, and I'm not sure why. I also do not get any errors on the console.
I also tried using:
ClickedFile.childNodes[0].select(); ClickedFile.childNodes[1].focus(); ClickedFile.focus();
None of them worked.
Edit:
I know that using jQuery can help, but I'm more interested in knowing why this is not working.
I fixed the problem. This has something to do with event handlers. My answer is posted below.
source share