javascript - jQuery.each on div in partial view always returning 1 -
in mvc index page, each element in viewmodel, renders via partial view.
i need run small script on each of these partial views , trying use jquery.each()
however, cannot seem iterate $get
returning 1, not actual number of elements in $get
.
index.cshtml
@model ienumerable<viewmodels.dummyvm> @{ viewbag.title = "index"; } <h2>index</h2> <div> @for (int = 1; <= 10; i++) { @html.partial("pv", i); } </div> @section scripts { <script type="text/javascript"> $(function () { alert($("#logentry").length); }) </script> }
pv.cshtml
@model int <div id="logentry"> log entry : @(model) </div>
for simplified test above, once page has .ready() iterates through , dumps console i'm getting log entry : 0
id
should unique in same document use classes instead :
@model int <div class="logentry"> log entry : @(model) </div>
then use class selector .
in js code :
$(function () { alert($(".logentry").length); })
if not edit pv.cshtml
page use :
$(function () { alert($("[id='logentry']").length); })
hope helps.
Comments
Post a Comment