This question gives more details about context and motivation. Notice that I am on Linux and cares only about recent Firefox (at least 38) & Chrome.
Basically, I want to edit some AST interactively with a web interface.
In the MELT monitor on github commit 7b869102332bd29309 I would like to have a focusable <span>
(which has tabindex='0'
so can get focus) which, when I press the spacebar, is replaced by some <input type='text'/>
which has already the focus...
I am not using contenteditable
anymore, see this, because it looks that contenteditable
is really messy (and don't work as well as I want)!
I've made a jsfiddle containing a simple example with:
<div id='mydiv_id'>
*before* <span id='myspan_id' tabindex='0'>in span</span> !after!
</div>
and the JQuery 2 code:
var $mydiv = null;
var $myspan = null;
$(document).ready(function() {
$myspan = $('#myspan_id');
$mydiv = $('#mydiv_id');
console.log (" mydiv=", $mydiv, " myspan=", $myspan);
$myspan.on("keypress", function (ev) {
console.log ("myspan keypress ev=", ev);
if (ev.keyCode === 32 || ev.key === ' ') {
console.log ("myspan got space ev=", ev);
var myinput =
$("<input type='text' id='myinput_id' width='16' class='myinp_cl'/>");
$myspan.replaceWith(myinput);
myinput.focus();
console.log ("myspan replaced with myinput=", myinput);
}
});
console.log (" mydiv=", $mydiv, " myspan=", $myspan);
});
but it does not work as expected.
Or perhaps a focused <span>
element cannot be replaced (on space keypress) with a focused <input>
element?
(in the MELT monitor, I'm using jquery 2.1.4 embedded inside)
addenda
the updated jsfiddle works (sorry for my mistake, it needs jquery 2.1.4, with which it is working -and I regret having asked the question here), and since the Javascript of the MELT monitor is AJAX generated, I am not seeing every error in the console (see this question).
NB: In commit df3bdf3984bc202f I now have a case when, after programmatically moving the focus to a newly created <input>
, $(':focus')
is an empty object, and document.activeElement
is the <body>
itself....
I am now tempted to delete this question, it is probably useless...