I am trying to make a link which when clicked turns into a text-box. The text-box is "focused" by default and when you click anywhere outside the text-box (thus making it "unfocused", it turns back to the original link. I am trying to use replaceWith() function in JQuery to do this, but couldn't get it correctly. The link turns into the text-box but and but for some reason which I am not properly understanding it doesn't turn back into the link when the text-box is "unfocused". Here is the fiddle that I've created to illustrate this. Could anyone tell me what I am doing wrong here.
The code that I've used is,
<script>
$(document).ready(function() {
$('a.edit').click(function() {
var removed = $(this).replaceWith("<input type='text' class='searchBox'/>");
$('input.searchBox').focus();
});
$('input.searchBox').focusout(function() {
$(this).replaceWith('<a class="edit" href="#">Edit</a>');
});
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
<div class="controls">
<a class="edit" href="#">Replace Link</a>
</div>
Thanks a lot. :)