javascript - JQuery .each not looping through all elements with class -


i working on navigation solution heavily customized pages hosted on content management platform employer uses.

i trying build function loops through elements specific class, reads data store value, loads external resource based on value container div.

while function works first match, stops @ point - no iteration through rest of document. function called in separate $(document).ready(function() block, have several functions running @ page load.

edit: had outdated code playing different things, current code below.

javascript:

 function loadnavigation() {  $('.toplevelnav').each(function () {  var toplevelnavindex = $(this).data('nav-index')+1;  var toplevelnavitem = spacemaincontent + " table tr:nth-child(" + toplevelnavindex + ") td:nth-child(2)";  $(this).children().first().load(toplevelnavitem); }); } 

html:

<div class="toplevelnav" data-nav-index="1">main  <div>container</div> <div class="sublevelnav" data-nav-index ="3">sub<div>container</div></div> <div class="sublevelnav" data-nav-index ="4">sub<div>container</div></div> <div class="sublevelnav" data-nav-index ="5">sub<div>container</div></div> <div class="sublevelnav" data-nav-index ="6">sub<div>container</div></div> <div class="sublevelnav" data-nav-index ="2">sub<div>container</div></div> </div> <div class="toplevelnav" data-nav-index ="2">main <div>container</div> <div class="sublevelnav" data-nav-index ="3">sub<div>container</div></div> <div class="sublevelnav" data-nav-index ="5">sub<div>container</div></div> <div class="sublevelnav" data-nav-index ="2">sub<div>container</div></div> </div> <div class="toplevelnav" data-nav-index ="3">main <div>container</div> <div class="sublevelnav" data-nav-index ="2">sub<div>container</div></div> </div> 

i cannot life of me figure out why stopping after first match, not smart man. thoughts?

if want process sublevelnavs, selector wrong. change either

$('.toplevelnav .sublevelnav').each(function () { 

if want iterate through sublevelnavs in top level one

or

$('.sublevelnav').each(function () { 

if want iterate through every sub level navigation


Comments