Skip to content
Advertisement

Why is my .empty() not emptying the parent element?

I cannot get the jQuery empty method to work on my appended HTML elements. It’s quite a simple problem seemingly, but it has me beat.

I’ve tried moving the empty method around in the code but I still cannot get it to empty.

Edit: It will not let me edit this unless there is more text so here is some more text.

My jQuery/JavaScript:

JavaScript

My HTML code:

JavaScript

Advertisement

Answer

It seems that you want to both append new data to your existing data while also deleting the old data. Your code is constantly referencing append, which implies that you want to keep the previous data, but your question asks about how to clear the holidays div, and then add the new holiday.

Answering your question, we don’t need an array if we’re always deleting the previous holiday information, instead we can use a JavaScript Object to hold the information. The other portion that you’ll notice I changed is I took out the for loop you had. Since we have a single holiday, we don’t need to iterate through an array. The following code should show how to work with a single holiday in an Object. Note that I did not change the HTML

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