1
Dear friends, I am a student and curious in the subject, not an experienced programmer, hence my question.
Come on, JQUERY’s "ON()" method can be associated with several types of events ("click", "mouseleave", etc.), including more than one at the same time, right?! Well, through the "ON" method and a "CLICK" event, I tried to associate a change in the "SRC" of the image (in the "ALT" and in the "TITLE", too). What you expected would happen, which was basically changing the image by clicking on it, happens unsatisfactorily, the image changes very quickly and does not stay with the change, as you would expect. It only occurs at the instant of the click and returns to what it was after releasing the mouse button.
Follow HTML, images and JQUERY for analysis. (Thanks for your help!)
HTML
<a href=""> <!-- Espanha --> <img class="img-esp" src="img/esp.png" alt="España" title="España" /></a>
IMAGERY (Flags)
JQUERY
$(document).ready(function() {
$(".img-esp").on("click", function() {
//Mudar a bandeirinha para a do Brasil (idioma português)
$(this).attr("src", "img/bra.png");
//Mudar o atributo ALT da bandeirinha (idioma português)
$(this).attr("alt", "Português (Brasil)");
//Mudar o atributo TITLE da bandeirinha (idioma português)
$(this).attr("title", "Português (Brasil)");
});
});
I believe this is due to the attribute
href
be present at the taga
, to resolve, change"click", function(e) { e.preventDefault(); ...
or just add areturn false;
or simply remove the attribute.– NoobSaibot
I removed A HREF and it worked! Thank you very much! Thanks!
– Alexandre Soares da Silva
What is "e. preventDefault();"? Sorry I’m curious to ask you.
– Alexandre Soares da Silva
preventDefault is used to change the default behavior of an element in html. https://jsfiddle.net/pq8Lkuso/3/ see if this is what you want. Just add <a href='javascript:void(0);'>
– Rafael Salomão
If <a> is not serving, you can remove it all and not just href.
– Sam
Thanks for the help, guys!
– Alexandre Soares da Silva