Skip to content

Instantly share code, notes, and snippets.

@narwanimonish
Created June 18, 2019 10:22
Show Gist options
  • Save narwanimonish/b3def0aad701cb4b0f27ae8f06c854db to your computer and use it in GitHub Desktop.
Save narwanimonish/b3def0aad701cb4b0f27ae8f06c854db to your computer and use it in GitHub Desktop.
Replace span tag with input to edit value & then value appear in span tag
<div class="span-input">
<span class="span-el">1234 ms</span>
<input type="text" class="hide">
</div>
<div class="span-input">
<span data-append=" ms" class="span-el">1234 ms</span>
<input type="text" class="hide">
</div>
<img src="https://via.placeholder.com/150x108" alt="">
$(function () {
var containerEl = $(".span-input");
var spanEl = containerEl.find("span.span-el");
var inputEl = containerEl.find("input");
spanEl.click(function (e) {
var el = $(this);
var val = el.text().split(" ")[0];
if (val) {
var currInput = el.siblings("input");
currInput.val(val);
el.addClass("hide");
currInput.removeClass("hide");
setTimeout(function () {
$("body").click(function (e) {
if ($(e.target).is(inputEl)) {
e.stopImmediatePropagation()
return false;
}
var dataAppend = el.data("append") ? el.data("append") : "";
el.text(currInput.val() + dataAppend);
currInput.addClass("hide");
el.removeClass("hide");
$("body").unbind("click");
})
}, 500)
}
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment