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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -