jquery - Check if has class -


this html:

<div id="gallery">     <div class="item"></div>     <div class="somethingelse"></div>     <div class="item"></div>     <div class="somethingelse"></div>     <div class="item active"></div>     <div class="somethingelse"></div>     <div class="item"></div> </div> 

now, each item want add number in div, did this:

function initmenu() {     $( ".gallery-menu" ).html("");     var columns = $('.item').length;     ( var = 0; < columns; i++ ) {         $( ".gallery-menu" ).append( );     } } initmenu(); 

now simple 0 1 2 3 in gallery-menu div good.

now want check if active. added check this:

function initmenu() {     $( ".gallery-menu" ).html("");     var columns = $('.item').length;     ( var = 0; < columns; i++ ) {         if ($(".item:eq(i)").hasclass('active')) {             $( ".gallery-menu" ).append( + "-active");         } else {             $( ".gallery-menu" ).append( );         }     } } initmenu(); 

but it's not working.. able point me in right direction?

a cleaner way use jquery's each() function:

$('.item').each(function(i, el){     if ($(this).hasclass('active')) {         $( ".gallery-menu" ).append( + "-active");     } else {         $( ".gallery-menu" ).append( );     } }); 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -