views:

193

answers:

1

Good morning all. I have a div that I'm able to save text to when I click a save button but what I would like is if the text would just automatically save on an onmouseout event. For example:

I have a div and two buttons (Save and Cancel). The user is currently able to mouse over the current text data inside the div, click on the text and they can then edit said text. The only way they can save this text is by clicking the save button. Is there a way to use onmouseout to save the text so that the user simply has to click and edit the text, then click away and have it saved? Thanks!

Ok, so the code below is what I have thus far but it just doesn't seem to work. I'm not worried about the save code b/c I already have that elsewhere. I'm just trying to get it to where I can type in the text, get onBlur to fire, and have the HTML revert back to what it looked like before I clicked the text. As of right now I can click it and the textbox within the div appears but when I click outside of the div, the textbox stays visible. Any ideas?

function setClickable() {
        $('#editInPlace').click(function() {
            var textarea = '<div onBlur="saveChanges()><textarea rows="3" cols="30">' + $(this).html() + '</textarea>';
            //var button = '<div><input type="button" value="SAVE" class="saveButton" /> OR <input type="button" value="CANCEL" class="cancelButton" /></div></div>';
            var revert = $(this).html();
            $(this).after(textarea).remove();           

        })
.mouseover(function() {
    $(this).addClass("editable");
})
.mouseout(function() {
    $(this).removeClass("editable");
    saveChanges(this, revert);
});
    };

    function saveChanges(obj, cancel) {
        if (!cancel) {
            var t = $(obj).parent().siblings(0).val();

            //need some post code for saves

        }
        else {
            var t = cancel;
        }
        if (t == '') t = '(click to add text)';
        $(obj).parent().parent().after('<div id="editInPlace">' + t + '</div>').remove();
        setClickable();
    }   
A: 

I think you want the onblur event as you mentioned clicking outside of the div (not just moving the mouse out):

<div contenteditable="true" onblur="saveContent()">
    Edit me and then click or tab out to save.
</div>

Whatever you have for your Save button's onclick event will go in the onblur event of the div. This will just save the content when the element loses focus - it defeats the point of having a cancel button though.

Andy E
I will give this a try.
Adam