The actual solution is to increase the min-width of the spans to allow the emptied span to still receive the click event being processed as it is processing the blur event e.g.
.simpletext {
outline: none;
min-width: 30px;
display: inline-block;
}
If you want the min-width to be calculated based on content, do this instead:
http://jsfiddle.net/TrueBlueAussie/10r2gasq/14/
var content = $(".simpletext");
content.focus(function() {
var $this = $(this);
if ($this.html() === "Text"){
// Set the min size to the current size before removing the text
$this.css('min-width', $this.innerWidth());
$this.html("");
}
console.log($(this)[0]);
});
content.blur(function() {
var $this = $(this);
if ($this.html() === ""){
// Set the min size back to its value in the CSS
$this.css('min-width', '');
$this.html("Text");
}
});
Note: Using text()
is still preferable to html()
but that was not a reliable solution on its own.
Something fishy:
The other answer, using text()
instead of html()
is actually a bit of a red-herring...
It only works differently with text()
(vs html()
), if the <br/>
follows immediately on from the span
.
e.g. only the first three entries in this example work!:
http://jsfiddle.net/TrueBlueAussie/10r2gasq/9
Basically using text()
will also fail if the <br/>
s are on a different lines following the spans, or if the min-width is not specified.
I am guessing that the <br/>
on the same line extends the range of the <span>
element when it is emptied which allows the click to propagate to the span. html()
just sets innerHTML behind the scenes so gives slightly different behaviour. The behaviour of click on elements being removed is a real grey area.
I created this JSFiddle with various options to see what was going on. With the 8px min-width of spans removed none of the options work:
http://jsfiddle.net/TrueBlueAussie/10r2gasq/15/