Mr A
Mr A

Reputation: 6768

Getting id's of particular class and storing it in an array using jquery

I wanted to save all the id's with the class inactive in an array , then i want to itereate through the array and check for particular values , for instance the inactive ids will be like : menu-level-0-0-1 , menu-level-0-0-2, menu-level-0-1-3 I want to save all these id's and then split it like:

        var inactiveID = $('.inactive').attr('id')

        var splitInactive = inactiveID.split('-');
        var inactiveParentCategoryid = splitInactive[3];
        var inactiveCategoryid = splitInactive[4];

After splitting it do some checking and if the check pass add the class:

if (parentCategoryid == inactiveCategoryid) {
            //$('.menu-level-0-0-'+ inactiveCategoryid).css('background-image', 'url(/Themes/DarkOrange/Content/images/cat-ul-active.png)');
          $('.inactive ').css('color', 'white !Important');
        }

The active ids will only be one , thats why i dont need an array and can easily be accessible by :

 var activeId = $('.active').attr('id')
        var split = activeId.split('-');
        var parentCategoryid = split[3];
        var categoryid = split[4];

Can any one help me in sorting out this issue in a correct way or any other suggestions will be highly appreciated.

Upvotes: 0

Views: 178

Answers (2)

ScottE
ScottE

Reputation: 21630

The map function is well suited to this task:

    var items = $('.iactive').map(function () {
        return this.id;
    }).get();

items is now an array.

Upvotes: 4

David Laberge
David Laberge

Reputation: 16031

You might want to use .each()

var activeId = new Array();
$('.active').each(function(){
   var currentId = this.id;
   var splitedId = currentId.split('-');
   activeId.push($(this).id); 
});

Upvotes: 1

Related Questions