Take class name and jquery ID

Asked

Viewed 26,624 times

1

Guys I have the following situation, I do a search on an object behind some classes, as we know the FIND() returns what is inside the selected class, using join with find o each, traversing the whole object.

But the situation I have, I must list all (Ids and classes) descendants of the find classes, I have to list the names of these classes and ids:

$(objeto).find('.nome1, .nome2').each(function(index, value){


});

Imagine, if you have any ID (id="meunome" or class="test" etc.) I have to list all this, I have no idea how many descending elements I have, I don’t know everyone’s name, because there are more than 100 WEB pages inside the object...

I know that if I knew the kind of element, I could do:

$(this).attr('id');
$(this).attr('class');

What I need is to verify which names of the classes and Ids I have in the object... from where I searched with find...

Someone has a way for me to do it...

  • .name1 and . Nome2 are fixed or you do not know?

1 answer

8


"*" looks for everything.

If you don’t know the classes:

var arrayObjetos = [];

$(objeto).find('*').each(function(){
    var classe = $(this).attr("class");
    var id = $(this).attr("id"); 
    arrayObjetos .push({classe:classe, id:id});
});

If you want to filter even by the classes . name1 and . name2:

var arrayObjetos = [];

$(objeto).find('.nome1 *, .nome2 *').each(function(){
    var classe = $(this).attr("class");
    var id = $(this).attr("id"); 
    arrayObjetos .push({classe:classe, id:id});
});

Finally on the console you see the result:

console.log(arrayObjetos);
  • 2

    No need to create new jquery objects to get these properties, better use direct this.id and this.className

  • Thanks was just that.. I didn’t really ""know"" the *, in fact its usefulness in Jquery... Just think about the reset of the CSS (*) ... Mysql SELECT *... ATT

  • Truth @bfavaretto, I got used to jQuery and certain pure js commands I don’t know by heart, but great tip.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.