I want to delete an empty DOM with jQuery


jQuery doesn’t judge empty for some reason

There was an article in wordpress that I wanted to write using a table, but when I made a table, for some reason it was only for a specific table

Is added …
It would be nice if it was just added, but since it has a width, it just swells up there.
I didn’t know about the phenomenon that was added without permission, so I can’t help it, so I’m going to use jQuery to quickly erase only the empty elements.
When I researched various things, I found a lot of sample sources because there are many smart people in the world.
Among them, there was something that looked good.

$ empty= $ (‘td’);
if ($ .trim ($ empty.text ())==”) {
console.log (’empty’);

Put the td element in the variable, trim the extra character string, and finally if the text is”””, it means that there is nothing in td.
It’s simple, but it looks very good, so I’ll repeat it for the number of td.

$ (function () {
for (var i= 0; i & lt; $ (‘td’). length; i ++) {
$ tmp= $ (this);
if ($ .trim ($ tmp.text ())==”) {
console.log (’empty’);

I haven’t deleted it right now, but if there is an empty element, it will be caught in the condition and empty will be returned!

JQuery doesn’t show empty for some reason

I’m not very confident in javascript, so I thought it might not be working, so I added it so that I could count it repeatedly.
As a result, it works as many as td.

That means that it is not caught in the judgment of if, it is a mystery.

However, looking at the contents of HTML here, a problem was discovered.
Even if an empty element can be found, there is an empty td among the elements that are normally used as a table.
That’s right, if you’re using a long table, the cells may be empty.
Since there is no help for it, I decided to erase it one by one in a straightforward manner as shown below.

$ (‘Class’) .find (‘td:last’). remove ();

Fortunately, the part that is added in common is using a slightly special class, which was the last td element, so I just wrote a few of them for each class and it disappeared.
The mystery still remains, but I was able to achieve what I wanted to do for the time being.
Anyway, why is the mystery element added?