fill table with javascript and ajax!

Asked

Viewed 162 times

-2

I’m trying to fill a table using javascript with the data coming from an ajax model. of that fortress :

$("#md").on("click", function() {
                if (FA.checked) {
                    $("div.hidden").css('display', 'block');
                    $.ajax({
                        url: "/treino",
                        method: "GET",
                        contentType: 'application/json; charset=utf-8',
                        success: function(resp) {
                            $('#score1').append(resp.scoreF);                    
                        }
                    })

                }
            })

The code is working! when I press the button my table in html adds the expected value in the waiting location with the append function , the problem is that when I press the button again the table adds the value again , getting two values one next to the other , i would like a function that simply replaces one value with another !

  • 1

    Tricky to help you because the error is local, you need to show us with a minimal example, only this code does not help us to help you!

  • 1

    Thank you for answering, I will try to reformulate the question being clearer and objective !

1 answer

1


The function .append() serves to add content. In your case, if you want to place a content replacing, by chance, one that already exists, you can use one of two functions, depending on the content returned by AJAX:

.html(): whether the content has HTML code or plain text, or

.text(): if the content has only plain text.

Then it would be $('#score1').html(resp.scoreF); or $('#score1').text(resp.scoreF);, depending on the return you expect in AJAX,

  • Thank you, it worked !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.