1

I'm using a jQuery with a OnClick event to show 3 dropdown menus. The problem is, when I go to another page and come back, the jQuery gets called twice, and stays called until I reload. This means that if I leave and enter again, the jQuery gets called 3 times now.

Is there anyway to prevent the jQuery to get called multiple times?

Edit: Forgot the code of the jQuery

 jQuery('body').on('click', '.headerDetalhe', function() {

           $headerDetalhe = jQuery(this);

           var ExpandableTitulo = this.getAttribute('data-Titulo');
           var ExpandableParent = this.getAttribute('data-Parent');
           var ExpandableCurrente = this.getAttribute('data-Currente');
           var ExpandableID = this.getAttribute('data-ExpandableID');
           var ExpandableICON = this.getAttribute('data-Icon')

           $contentDetalhe = $headerDetalhe.next();

           if(jQuery(this).hasClass('ActiveExpandable')) {
               if((ExpandableParent > 0) && (ExpandableCurrente > 0)){
                   jQuery(this).removeClass('ActiveExpandable');
                   jQuery(this).removeClass('FirstExpandable');
                   var ExpProd = document.querySelectorAll('[id^="ExpandableID"]');
                       ExpProd = [].slice.call(ExpProd);
                       ExpProd.forEach(function (elem) {
                           var Titulo = elem.dataset.titulo;
                           var Parent = elem.dataset.parent;
                           var Currente = elem.dataset.currente;
                           var ExpID = elem.dataset.expandableid;
                           var Icon = elem.dataset.Icon;

                           /*if((ExpandableParent == Parent) && (Currente == 0)){
                               document.getElementById("IDExpContainer" + ExpID).style.display = "none";
                           }*/

                   });
                   $contentDetalhe.slideToggle(500, function () {
                       $headerDetalhe.html(function () {
                           return $contentDetalhe.is(":visible") ? "<span class='TextExpandableAtivo " + ExpandableICON + "' id='Text" + ExpandableID + "'> Comunicados de Imprensa <span class='SinalExpandableAtivo' id='IDSignal" + ExpandableID + "'>-</span></span>" : "<span class='TextExpandable " + ExpandableICON + "' id='Text" + ExpandableID + "'>" + ExpandableTitulo + "<span class='SinalExpandable' id='IDSignal" + ExpandableID + "'>+</span></span>";
                       });
                   });

               }
               else{
                   jQuery(this).removeClass('ActiveExpandable');

                   $contentDetalhe.slideToggle(500, function () {
                       $headerDetalhe.html(function () {
                           return $contentDetalhe.is(":visible") ? "<span class='TextSubExpandableAtivo " + ExpandableICON + "' id='Text" + ExpandableID + "'>" + ExpandableTitulo + "<span class='SinalExpandableAtivo' id='IDSignal" + ExpandableID + "'>-</span></span>" : "<span class='TextSubExpandable " + ExpandableICON + "' id='Text" + ExpandableID + "'>" + ExpandableTitulo + "<span class='SinalExpandable' id='IDSignal" + ExpandableID + "'>+</span></span>";
                       });
                   });

               }
           }
           else{
               if((ExpandableParent > 0) && (ExpandableCurrente > 0)){
                   jQuery(this).addClass('ActiveExpandable');
                   var ExpProd = document.querySelectorAll('[id^="ExpandableID"]');
                       ExpProd = [].slice.call(ExpProd);
                       ExpProd.forEach(function (elem) {
                           var Titulo = elem.dataset.titulo;
                           var Parent = elem.dataset.parent;
                           var Currente = elem.dataset.currente;
                           var ExpID = elem.dataset.expandableid;

                            if((ExpandableParent == Parent) && (Currente == 0)){
                               document.getElementById("IDExpContainer" + ExpID).style.display = "block";
                            }

                   });

                   $contentDetalhe.slideToggle(500, function () {
                       $headerDetalhe.html(function () {
                           return $contentDetalhe.is(":visible") ? "<span class='TextExpandableAtivo " + ExpandableICON + "' id='Text" + ExpandableID + "'>" + ExpandableTitulo + "<span class='SinalExpandableAtivo' id='IDSignal" + ExpandableID + "'>-</span></span>" : "<span class='TextExpandable " + ExpandableICON + "' id='Text" + ExpandableID + "'>" + ExpandableTitulo + "<span class='SinalExpandable' id='IDSignal" + ExpandableID + "'>+</span></span>";
                       });
                   });

               }
               else{
                   jQuery(this).addClass('ActiveExpandable');

                   $contentDetalhe.slideToggle(500, function () {
                       $headerDetalhe.html(function () {
                           return $contentDetalhe.is(":visible") ? "<span class='TextSubExpandableAtivo " + ExpandableICON + "' id='Text" + ExpandableID + "'>" + ExpandableTitulo + "<span class='SinalExpandableAtivo' id='IDSignal" + ExpandableID + "'>-</span></span>" : "<span class='TextSubExpandable " + ExpandableICON + "' id='Text" + ExpandableID + "'>" + ExpandableTitulo + "<span class='SinalExpandable' id='IDSignal" + ExpandableID + "'>+</span></span>";
                       });
                   });
               }
           }

       });

I think there are some confusions here, after leaving and enter, the dropdown after being clicked, it will open and then close (being called twice).

Thank you

3
  • 1
    Can you please add the code you are using to your question? Oct 16, 2019 at 10:25
  • api.jquery.com/one Oct 16, 2019 at 10:33
  • This doesn't work. I'm using 3 dropdown columns. Using 'one', just let me open one dropdown, and then doesn't let me open or close any of them, and that's not my problem.
    – João
    Oct 16, 2019 at 10:40

0

Browse other questions tagged or ask your own question.