Just access the element son of the event target using the children
and use the methods addClass
and removeClass
to exchange classes:
$('.botao').on('click', function() {
$(this).children('i').removeClass('fa-bolt').addClass('fa-spinner');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<a class="btn btn-primary botao">
<i class="fa fa-fw fa-bolt"></i> Importar
</a>
PS: I don’t know if you noticed, but I added the class fa-fw
to HTML. This is to make both icons the same size, avoiding an unpleasant transition in the user’s eyes. ;)
Edit:
If you, for any reason, need to define the Listener of event in HTML, you can do so:
function importar_txt_cnes(){
$('.botao > i').removeClass('fa-bolt').addClass('fa-spinner');
}
Note, however, that defining events directly in HTML is not a good practice. Always prefer to define them directly in the Javascript code, as exemplified in the first code snippet of this answer.
hi buddy, need to be called the function
onclick="importar_txt_cnes()"
because in it I will send parametres in each boot– Italo Rodrigo
I tested your code inside the function I created and it doesn’t work. I need to modify something?
– Italo Rodrigo
I changed the question by showing how I’m using the code you gave me
– Italo Rodrigo
I edited the answer adding more details...
– Luiz Felipe