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.