skip to Main Content

I have a jQuery each loop to loop through elements but it is not updating once a status has been changed.

<span class="btn btn-icon btn-rounded btn-info social_selector" data-network="twitter" data-account-id="1" data-active='1'>
<i class="fa fa-twitter"></i>
</span>
<span class="btn btn-icon btn-rounded indigo social_selector" data-network="facebook" data-account-id="1" data-active='1'>
<i class="fa fa-facebook"></i>
</span>
<span class="btn btn-icon btn-rounded btn-warning social_selector" data-network="instagram" data-account-id="1" data-active='1'>
<i class="fa fa-instagram"></i>
</span>

The jQuery loop is as follows;

//Social Selector
    $(document).on('click','.social_selector',function(){

        if($(this).attr('data-active') == 1){
            $(this).addClass('btn-disabled');
            $(this).attr('data-active', 0);
        }else{
            $(this).removeClass('btn-disabled');
            $(this).attr('data-active', 1);
        }

        var networks = "";
        $(".social_selector").each(function(index, element){
            if($(element).data('active') == "1"){
                networks += $(element ).data('network')+', ';
            }
        });
        $('#social_network_holder').val(networks);

    });

The data-active is changed on each click when the jQuery loop is then ran so I am not sure if it is because the Dom is being updated between the loops?

//Social Selector
$(document).on('click', '.social_selector', function() {

  if ($(this).attr('data-active') == 1) {
    $(this).addClass('btn-disabled');
    $(this).attr('data-active', 0);
  } else {
    $(this).removeClass('btn-disabled');
    $(this).attr('data-active', 1);
  }

  var networks = "";
  $(".social_selector").each(function(index, element) {
    if ($(element).data('active') == "1") {
      networks += $(element).data('network') + ', ';
    }
  });
  $('#social_network_holder').val(networks);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
<span class="btn btn-icon btn-rounded btn-info social_selector" data-network="twitter" data-account-id="1" data-active='1'>
    <i class="fa fa-twitter"></i>
    </span>
<span class="btn btn-icon btn-rounded indigo social_selector" data-network="facebook" data-account-id="1" data-active='1'>
    <i class="fa fa-facebook"></i>
    </span>
<span class="btn btn-icon btn-rounded btn-warning social_selector" data-network="instagram" data-account-id="1" data-active='1'>
    <i class="fa fa-instagram"></i>
    </span>

3

Answers


  1. replace data() method with attr() method

       $(".social_selector").each(function(index, element){
            if($(element).attr('data-active') == "1"){
                networks += $(element ).data('network')+', ';
            }
        });
    
    Login or Signup to reply.
  2. Change your code to the below which uses .data('active') instead of .attr('data-active') or you could change them all to .attr('data-active'), either way , just make sure they are all the same, you have one that is different:

       $(document).on('click','.social_selector',function(){
            var $this=$(this);
            
            if($this.data('active') == 1){
                $this.addClass('btn-disabled');
                $this.data('active', 0);
            }else{
                $this.removeClass('btn-disabled');
                $this.data('active', 1);
            }
    
            var networks = "";
            $(".social_selector").each(function(index, element){
                if($(element).data('active') == 1){
                    networks += $(element ).data('network')+', ';
                }
            });
            $('#social_network_holder').val(networks);
    
        });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <span class="btn btn-icon btn-rounded btn-info social_selector" data-network="twitter" data-account-id="1" data-active='1'>twitter
    <i class="fa fa-twitter"></i>
    </span>
    <span class="btn btn-icon btn-rounded indigo social_selector" data-network="facebook" data-account-id="1" data-active='1'>facebook
    <i class="fa fa-facebook"></i>
    </span>
    <span class="btn btn-icon btn-rounded btn-warning social_selector" data-network="instagram" data-account-id="1" data-active='1'>instagram
    <i class="fa fa-instagram"></i>
    </span>
    <input class="form-control" required="required" id="social_network_holder" name="social_networks" type="text">
    Login or Signup to reply.
  3. Check out my DEMO .

    Just check if you require this

    $(document).on('click','.social_selector',function(){
    
            if($(this).attr('data-active') == 1){
                $(this).addClass('btn-disabled');
                $(this).attr('data-active', 0);
            }else{
                $(this).removeClass('btn-disabled');
                $(this).attr('data-active', 1);
            }
    
            var networks = "";
            $(".social_selector").each(function(index, element){
                if($(element).attr('data-active') == 1){
                    networks += $(element ).attr('data-network')+', ';
                }
            });
            $('#social_network_holder').val(networks);
    alert(networks)
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search