Go through inputs with jquery and check if there is a certain class in the input

0

I'm getting the inputs with the code var itensTabelaPresentation = ('#tabelaPresentarion > li'); and would like to go through them and go check if they have a certain class, how do I do this in a foreach?

<ul class="nav nav-tabs" id="tabelaPresentarion">
	<li role="presentation" id="tabGeral" class="active"><a href="#">Geral</a></li>
	<li role="presentation" id="tabAuditoria" ><a href="#">Audiroria</a></li>
</ul>

<script>
	jQuery(document).ready(function () {

		var tabGeral = $('#tabGeral');
		var tabAuditoria = $('#tabAuditoria');

		var painelGeral = $('#painelGeral');
		var painelAuditoria = $('#painelAuditoria');

		var itensTabelaPresentation = ('#tabelaPresentarion > li');
		
		tabGeral.click(function () {
			painelGeral.show();
			painelAuditoria.hide();

			for (var i = 0; i <= itensTabelaPresentation.length; i++) {
				
			}

		});

		tabAuditoria.click(function () {
			painelGeral.hide();
			painelAuditoria.show();
		});
		
	});
</script>
    
asked by anonymous 20.10.2017 / 19:24

3 answers

0

You can do this with JQuery:

$('.nav.nav-tabs li').each(function(){
    if($(this).attr('class') == "active"){
        alert("Achei a class!");
    } else {
        alert("A class não está aqui!");
    }
}); 

NOTE: Instead of active for the class you want to find.

    
20.10.2017 / 19:59
2

As you already have the list of elements that you will use to filter the class, var itensTabelaPresentation = ('#tabelaPresentarion > li'); , just use the filter method:

var soOsComClasse = itensTabelaPresentation.filter('.suaClasse');

If you need to cross all items yourself, just use what you said, each() , plus hasClass()

itensTabelaPresentation.each(function(index) {
  console.log('elemento ' + index + ' tem classe? ' + $(this).hasClass('suaClasse'));
});

More information:

20.10.2017 / 19:44
2

Just use .hasClass () for this.

$("#tabelaPresentarion > li").each(function(index) {
  if ($(this).hasClass('active'))
    console.log("O elemento " + index + ": " + $(this).text() + " está ativo.");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ulclass="nav nav-tabs" id="tabelaPresentarion">
  <li role="presentation" id="tabGeral" class="active"><a href="#">Geral</a></li>
  <li role="presentation" id="tabAuditoria"><a href="#">Audiroria</a></li>
</ul>
    
20.10.2017 / 20:00