Hi guys i am trying to use value of $.each outside the loop but whenever i save the 'id' it shows me the same data again so how can i access that value outside the loop. let me show you my code.
Now when i try to access the value of $post_id in like function it shows me the same id again for a certain loop. like if i got the id as 464 then for all the post_id it will be 464 but when i append the id it shows me the original id again
what can i do to access the value of id in like function ?
    function loadmore(){
          var lastID = $('.load-more').attr('lastID');
         //alert(lastID);
         
              jQuery.ajax({
                  type:'POST',
                  url:'<?php echo base_url("user/get_all_post"); ?>',
                   data: {id:  lastID },
                      dataType: 'json', 
                
                
                  beforeSend:function(data){
                      $('.load-more').show();
                  },
                  success:function(data){
                         var ParsedObject = JSON.stringify(data);            
                         var json = $.parseJSON(ParsedObject);
                          
                         
                         if (json=="") {
                          $("#bottom").append('<div class="btn btn-default col-md-6" >'+'No More Results'+'</div>');
                          $("#Load_more_data").hide()
                        
                         }else{
                           $postID=json[json.length-1].id;
                            
              $('.load-more').attr('lastID', $postID);
                $.each(json, function (key, data) {
   $post_id=data.id;
 
   var post_id=data.id;
   $('.post_id_value').attr('post_id', $post_id);
    var post_status=data.status;
     var status_image=data.status_image;
    var multimage=data.multimage;
  
                             if(!post_status=="" && !status_image==""){
                               $("#status_data").append('<input type="text" value="'+ post_id+'" class="post_id_value"> <div class="col-md-6 postdata"><a ><?php echo img($user_image); ?></a><a class="weshare_user_name text-font"><?php echo $uname; echo " "; echo $lname;?></a><div class="weshare_user_status">'+post_status+'</div><div class="weshare_user_singleimage"><img style="height:300px; width:400px;" src="<?php echo base_url('uploads'); ?>/'+status_image+'"></div><div class="row"><div class="col-md-12"><ul class="list-inline"><li><a href="#"><span class="glyphicon glyphicon-thumbs-up"></span> Like</a></li><li><a  onclick="like(this);"><span class="glyphicon glyphicon-comment"></span> Comment</a></li><li><a href="#"><span class="glyphicon glyphicon-share-alt"></span> Share</a></li></ul></div></div></div>');
                             }else{
                             }
                              if(!post_status=="" && status_image==""){
                               $("#status_data").append('<input type="text" value="'+ post_id+'" class="post_id_value"><div class="col-md-6 postdata" ><a ><?php echo img($user_image); ?></a><a class="weshare_user_name text-font"><?php echo $uname; echo " "; echo $lname;?></a><div class="weshare_user_status">'+post_status+'</div><div class="row"><div class="col-md-12"><ul class="list-inline"><li><a  onclick="like(this);"><span class="glyphicon glyphicon-thumbs-up"></span> Like</a></li><li><a ><span class="glyphicon glyphicon-comment"></span> Comment</a></li><li><a href="#"><span class="glyphicon glyphicon-share-alt"></span> Share</a></li></ul></div></div></div>');
                             }else{
                             }
                             
                             if (multimage=="") {
                             }
                              else{
                               $("#status_data").append('<input type="text" value="'+ post_id+'" class="post_id_value"><div class="col-md-6 postdata" ><a ><?php echo img($user_image); ?></a><a class="weshare_user_name text-font"><?php echo $uname; echo " "; echo $lname; ?></a><div class="weshare_user_multimage"><img style="height:300px; width:400px;" src="<?php echo base_url('uploads'); ?>/'+multimage+'"></div><div class="row"><div class="col-md-12"><ul class="list-inline"><li><a  onclick="like(this);"><span class="glyphicon glyphicon-thumbs-up"></span> Like</a></li><li><a href="#"><span class="glyphicon glyphicon-comment"></span> Comment</a></li><li><a ><span class="glyphicon glyphicon-share-alt"></span> Share</a></li></ul></div></div></div>');
                              }                              
                    });
                  }
              }
            });
          }
function like() {
  var Post_id = $('.post_id_value').attr('value');
  var User_id = $('.id_data').attr('value');
  alert(Post_id);
  jQuery.ajax({
    type: 'POST',
    url: '<?php echo base_url("user/post_likes"); ?>',
    data: {
      Post_id: Post_id,
      User_id: User_id
    },
    dataType: 'json',
    success: function(data) {
      console.log(data);
      alert();
    }
  });
}
here is the html part <div class="col-md-6 postdata">
  <a><img src="http://localhost/P_Display/uploads/camera-581126_1920.jpg" class="weshare_user_image" alt="" width="50px" height="50px"></a><a class="weshare_user_name text-font">Ashish Vyas</a>
  <div class="weshare_user_status">hi</div>
  <div class="row">
    <div class="col-md-12">
      <ul class="list-inline">
        <li><a onclick="like();"><span class="glyphicon glyphicon-thumbs-up"></span> Like</a></li>
        <li><a><span class="glyphicon glyphicon-comment"></span> Comment</a></li>
        <li><a href="#"><span class="glyphicon glyphicon-share-alt"></span> Share</a></li>
      </ul>
    </div>
  </div>
</div> here is the like button and whenever i click on it runs the ajax function like();
here is the like button and whenever i click on it runs the ajax function like(); 
    