I'm trying to check if a form is out of focus, so I could manipulate the DOM accordingly.
I've tried something like this:
$("#dialog-form").on("focusout", function () {
if ($(document.activeElement).parents('#dialog-form').length === 0) {
//Do some stuff...
};
});
Because focusout
binds to all the children elements, this event will fire even when I move the focus between elements in the form. Problem is, length always returns 0 so... obviously this is wrong.
Is there a best practice on the subject?
Edit: (cshtml)
@model IEnumerable<TasksWebApp.Task>
<h2>Tasks Managment Tool</h2>
<hr />
<br />
<ul id="MainUL">
@{foreach (var item in Model)
{
<li id="li_@item.Name">
<div class="taskContainer">
@if (item.SubTasks.Count > 0)
{
<div class="expand">
<button>Expand</button>
</div>
}
<div class="erase">
<button>Erase Task</button>
</div>
<div class="addSubClass">
<button>Add Sub-Class</button>
</div>
<div class="taskDetails">
Name: <span>@item.Name</span>
Owner: <span>@item.Owner</span>
</div>
</div>
<ul></ul>
</li>
}
}
</ul>
<div id="AddMainItem">
<button>Add Main Item</button>
</div>
<div id="dialog-form" title="Create New Task">
<p class="formHeadPar">All form fields are required:</p>
<p id="FormComments"></p>
<form id="AddTaskForm">
<label for="newTaskName">Name:</label>
<input type="text" name="newTaskName" id="newTaskName" placeholder="Task Name" />
<label for="newTaskOwner">Owner:</label>
<input type="text" name="newTaskOwner" id="newTaskOwner" placeholder="Task Owner" />
<input type="submit" tabindex="1" value="Add Task" />
<button id="CloseForm">Close Form</button>
</form>
</div>