1

I have this Code where i just select two players and i show some stats of those players. I'm Using Just A regular table to show the data (image1);

My Problem is , after i select another player , instead of updating the data from the new player i selected , the table just create another line (image2). all the data that show's are correct , but i dont want it to create another line on the table , i want to erase the past player and update to the current player but i dont know how i can acomplish that.

I'm Not the best with jquery , that's my code

<script>
        $(document).ready(function() {
//The First player Select

            jQuery("#p1").on('focusout', function(e) {

                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    }
                });
                var p1 = $('#p1').val();
                var p2 = $('#p2').val();    

                jQuery.ajax({
                    url: "{{ route('ajax') }}/?p1=" + p1 + "&p2=" + p2,
                    method: 'Get',

                    success: function(result) {

                        $("#player1").append("<h2 class='p1'>" + result[0].player + "</h2>");
                        $("#p1kills").append("<h2 class='alignRight'>" + result[0].kp + "</h2>");
                        $("#p1kda").append("<h2 class='alignRight'>" + result[0].kda + "</h2>");
                        $("#p1cspm").append("<h2 class='alignRight'>" + result[0].cspm + "</h2>");
                        $("#p1gpm").append("<h2 class='alignRight'>" + result[0].gpm + "</h2>");
                        $("#p1dpm").append("<h2 class='alignRight'>" + result[0].dpm + "</h2>");
       

                    }
                });




            });

//The Second player Select
            jQuery("#p2").on('focusout', function(e) {

                $.ajaxSetup({
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                    }
                });
                var p2 = $('#p2').val();
                var p1 = $('#p1').val();

                jQuery.ajax({
                    url: "{{ route('ajax') }}/?p1=" + p1 + "&p2=" + p2,
                    method: 'Get',

                    success: function(result) {
                        $("#player2").append("<h2 class='p1' >" + result[1].player + "</h2>");
                        $("#p2kills").append("<h2 >" + result[1].kp + "</h2>");
                        $("#p2kda").append("<h2 >" + result[1].kda + "</h2>");
                        $("#p2cspm").append("<h2 >" + result[1].cspm + "</h2>");
                        $("#p2gpm").append("<h2 >" + result[1].gpm + "</h2>");
                        $("#p2dpm").append("<h2 >" + result[1].dpm + "</h2>");


                    }
                });




            });

        });


    </script>

enter image description here

enter image description here

2
  • 1
    HI, use .html() instead of .append(). Commented Jun 24, 2021 at 4:31
  • as simple as it should be , Thanks , It's working !!! Commented Jun 24, 2021 at 4:34

1 Answer 1

1

You have to replace this

.append()

Into

.html()

i hope you got your solution thanks .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.