Active not being selected to the click

0

Good morning,

I know it should be a basic thing, but I'm having trouble with it.

The site contains two languages, when clicking on a flag, the language of the site changes and the flag for the language is colored, while the other flag is in Black and White, but when I click on a flag, it is selected and updates the page automatically, returning to the other flag. Could someone help me?

HTML

             <div class="languages">
               <a href="<?=URL_SITE?>/idiomas/changeLanguage/portugues">
                   <img id="br" class="active" src="<?=ICONES?>br-flag.png">
                </a>

                <a href="<?=URL_SITE?>/idiomas/changeLanguage/espanhol">
                    <img id="es" src="<?=ICONES?>es-flag.png">
                </a>
            </div>

SCRIPT

<script type="text/javascript">
        $(document).ready(function() {
            $('img').click(function() {
              $('img.active').removeClass("active");
              $(this).addClass("active"); 
            });
        });
</script>

CSS

.languages img{
         margin-left: 3px;
         filter: grayscale(1);
         -webkit-filter: grayscale(1);
      }

.languages img:hover, .languages img.active{
         filter: grayscale(0);
         -webkit-filter: grayscale(0);
      }
    
asked by anonymous 15.09.2016 / 13:37

1 answer

0

Since you redirect to another page / url you do not need to have logic in the client and you can do everything in PHP. Even what you have now in jQuery you can do on the server.

It would look something like this:

<?php
    $lang = $_GET['lang'];    
?>
<div class="languages">
    <a href="<?=URL_SITE?>/idiomas/changeLanguage/portugues?lang=br">
        <img id="br" class="<?= $lang == 'br' ? 'active' : ''?>" src="<?=ICONES?>br-flag.png" />
    </a>

    <a href="<?=URL_SITE?>/idiomas/changeLanguage/espanhol?lang=es">
        <img id="es" class="<?= $lang == 'es' ? 'active' : ''?>" src="<?=ICONES?>es-flag.png" />
    </a>
</div>
    
15.09.2016 / 13:50