Chris Hammond Chris Hammond - 6 months ago 22
Javascript Question

jQuery 'find' undefined

I have a set of nested

<div>
and on completion of the page load need to take action on the inner
<div>
of each of the outer
<div
>

In the
.ready()
of the page, I iterate through each of the
$('.outer').each()
calling the
doSomething()
function, which then attempts to
find()
the
<div class='inner'>
.

The problem is that the
.find()
method is failing with


Object doesn't support property or method 'find'


Below is the view source (from IE11) to demonstrate the issue.

Markup

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<div class="outer">
<div class="inner">
1
</div>
</div>
<div class="outer">
<div class="inner">
2
</div>
</div>
<div class="outer">
<div class="inner">
3
</div>
</div>

<script src="Scripts/jquery-1.10.2.js"></script>
<script src="Scripts/jquery.fittext.js"></script>
<script src="Scripts/jquery.cycle.lite.js"></script>
<script src="Scripts/jquery.validate.js"></script>
<script src="Scripts/jquery.validate.unobtrusive.js"></script>
<script src="Scripts/bootstrap.js"></script>
<script src="Scripts/respond.js"></script>
<script src="Scripts/moment.js"></script>

<script type="text/javascript">
$(function () {
$(".outer").each(function () { doSomething(this); })
})

function doSomething(divO) {
debugger;
var divI = divO.find("inner");
console.log(divI);
}
</script>


When the debugger breaks, evaluating
divO.tagName
on IDE (Visual Studio) it says that it is in fact a
Div
tag

Answer

use:

var divI = $(divO).find(".inner");
Comments