javascript - Why no error when accessing a DOM element that doesn't exist? -
i have divs partial views in them. why reference div doesn't exist not show kind of error? example, have 1 taskn div right now:
<div id="task1"> @html.partial("~/views/partialviews/taskdoscommand.cshtml") </div> this jquery show div:
$('#task' + task.prestotasktype).show(); when task.prestotasktype 1, task1 div correctly displays. however, when task.prestotasktype 1, 2, nothing displays (which good), there no error; no error shows on web page, , nothing displays in chrome developer tools console:

shouldn't kind of error display when accessing dom element doesn't exist?
no, because jquery .show() all elements jquery object wraps. if that's no elements @ all, it.
that's precisely monad-like aspect of jquery makes useful: imagine code 'd have write if things didn't work way:
var $whatever = $(...); if ($whatever.length) $.dosomething(); this worse: need introduce variable (in order avoid waste) and conditional... gain exactly?
if want see jquery matched can .length above, perhaps using .filter in process.
Comments
Post a Comment