avoid duplicity when doing append [closed]

1

var mySpan = $('<span class="dropdown"></span>');
$('.header-top-registro>.menu-mobile>li').append(mySpan);
<script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>

<div class="header-top-registro">
  <div class="menu-mobile" style="right: 0px;">
    <li>
    </li>
  </div>
</div>

your help please I am using this code to insert a span into my html, the problem I have every time I refresh or resize starts to double the span, as I avoid duplicity, thanks

var mySpan = $('<span class="dropdown"></span>');
$('.header-top-registro>.menu-mobile>li').append(mySpan);
    
asked by Sixto Mujica 24.04.2018 в 01:36
source

1 answer

0

With the declaration of a variable you could control the execution of the append. You could initialize the variable with a value and at the time of executing the append you could change the value of the variable, thus ensuring that it is only executed on certain occasions or only once.

let  numSpan = 0;

    $( window ).resize(function() {

        var mySpan = $('<span class="dropdown"> append </span>');


        if ( $(window).width() < 1200 ){
            if( numSpan == 0 ){
                $('.header-top-registro>.menu-mobile>li').append(mySpan);
                numSpan += 1;
            }
            console.log(numSpan)
        }
    }); 

I hope it's of your use.

    
answered by 24.04.2018 / 05:52
source