Placing focus on the edited item when validation fails

Hi,

I have a tree and I allow the user to edit the text associated with the items in place. I perform validation using the onEdit event (state = 2). When validation fails, I would like the focus to be placed on the item being edited.

For example, I would like the following:

(1) User clicks a button to create a new item. This brings focus to item text and they can begin typing immediately.
(2) User enters no text and hits enter
(3) User receives alert - bad input. onEdit should reject the edit and the focus should be brought back to the item and they should be able to keep typing right away.

The problem is that while the item is in focus, the user cannot type, because the cursor is not inside the item’s text. They have to click the item text to keep typing.

Here is my code - I discovered by accident that if I throw an exception at the spot marked HERE, I can achieve what I want! But I shouldn’t have to throw an exception to do this:

tag_tree.attachEvent("onEdit", function (state, id, tree, value) {
    switch (state) {
        case 2:
            // before closing of editor, validate input
            debugger;
            value = $.trim(value);
            if (value.length == 0) {
                alert('Tag name cannot be empty. Please enter a name.');
                // HERE: throw "my exception"; // stop the edit and focus back on item text
                return false;
            }

            text = value.replace(/ /g, '_').replace(/[^a-zA-Z_0-9]+/g, '').toUpperCase().substr(0, 30);
            if (duplicate_tag(id, text)) {
                alert('Tag "' + text + '" already exists. Please enter a different name.');
                // HERE: throw "my exception";
                return false;
            }

            save_tag(id, text);
    }

    return true;
});

Thanks in advance for any help you can provide.

Hi
Here is a helpful code snippets:

tree.attachEvent("onEdit",function(state){ if(state==1){ var inp = this._editCell.span.firstChild; inp.select(); } else if (state == 2){ if (value == "" || value == " "){ alert('Please, type the name of item'); return false; } } return true })

I tried that solution, but for me it just highlights the item’s text box, but the cursor is not inside of the box, so I can’t type until I click on the text box first.

Any idea how I can programmatically place the cursor in the item’s text box?

Thanks.

I attach a working tested sample. Cursor is plasing in textbox.
tree_edit_select.rar (943 Bytes)