Skip to content
Advertisement

Extra elements getting appended during AJAX call on html page

I have a piece of code that refreshes an HTML table every 5 seconds using AJAX calls

I am basically emptying out the HTML table and then appending all of its data again every 10 seconds to achieve this

Something like this –

JavaScript

where _appendHere is the id attribute of the table

This is my HTML code – (the data is being passed from my Django view to this page)

JavaScript

The CSS –

JavaScript

And this is the javascript section –

JavaScript

The GET request is being made to this URL in a Django view which renders the same page –

JavaScript

The views.py file –

JavaScript

For some reason, this code appends the input tag (the search box) as well.. but does it only once

enter image description here

And I am not sure why this is happening

I tried putting the input tag in a different div but that also does the same thing

Any help would be highly appreciated!! Thanks!!

Advertisement

Answer

I suspect your AJAX response contains the full Django response each time – it looks like you also have all those line-breaks <br> as well as the search form in the AJAX response.

You need to create a version that just supplied the inner HTML you want to put inside the element (specifically, just the table rows).

JavaScript

With just the rows in the response, you can replace them “in one hit” with:

JavaScript

Alternatively, you can handle receiving the full response by loading it into jQuery and then selecting out the table specifically.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement