Q:

Onblur event is not firing for first time

I have an application, I need to update the number, for this, I planned like, when I click on the number container, iam converting it into an input field with jquery, then on blur I want to turn it off as plain text inside the container div and then to trigger ajax for updating it in DB, but for the first time blur even is not triggering
I mean, if I click for the forst time on the number, it is converting it to input field, and when I click out side(blur even), the blur function is not working, if I click again and click out side, then it is working fine.
Here is my code. 
   <div onclick="wrap($(this))">999999999</div>

<script>    
function wrap(sel)
                        {
                        sel.html('<input onblur="unwrap($(this));" value="'+sel.html()+'" />');
                        sel.attr('onclick','');
                        }
                        function unwrap(child)
                        {
                        alert('Unwrapping');
                        }
</script>

Any help/suggestion would be helpful.

A:

This is because when you click on the div the first time, the input box is not given focus. Without focus, there is no blur. Try to give it focus after creating it...
function wrap(sel) {
    sel.html('<input onblur="unwrap($(this));" value="' + sel.html() + '" />');
    sel.attr('onclick', '');
    $("input").focus();
}

Fiddle: http://jsfiddle.net/Pwb4y/

