How I cope with HTML tables.

๐ŸŽ™๏ธ jj4th ยท 698 points ยท Posted at 20:58:35 on June 11, 2016 ยท (Permalink)


HTML Tables

HTML tables are annoying. When I work with HTML tables I get annoyed. I made this bookmarklet to cope.

TK-427 ยท 56 points ยท Posted at 01:55:01 on June 12, 2016 ยท (Permalink)

So this is the pivot table all my coworkers keep talking about

idle_zealot ยท 98 points ยท Posted at 02:06:16 on June 12, 2016 ยท (Permalink)

OP posted their code already, but it pulls in the entire Font Awesome CSS. Here's a lighter version:

javascript:style=document.createElement("style");style.innerHTML=".my-spin {transition: 1s linear;transform: rotate(360deg);}";document.getElementsByTagName("head")[0].appendChild(style);function toggleSpin(){document.querySelectorAll("table").forEach(function(a){a.classList.toggle("my-spin")})}toggleSpin();setTimeout(toggleSpin,2E3);

Remember to name it

(โ•ฏยฐโ–กยฐ)โ•ฏ๏ธต โ”ปโ”โ”ป
brown_monkey_ ยท 10 points ยท Posted at 06:47:44 on June 12, 2016 ยท (Permalink)

document.querySelectorAll("table").forEach(...)

That shouldn't work. Query selector all returns a node list, not an array, and there is no forEach method on it.

1lann ยท 16 points ยท Posted at 08:57:28 on June 12, 2016 ยท (Permalink)

Was added in Chrome version 51 released on May 25th 2016. Change was committed on March 15th 2016:

In addition to the above interfaces, the NodeList interface is granted the methods "forEach", "keys", "values", and "entries", due to the iterable<Node> definition in https://dom.spec.whatwg.org/#interface-nodelist. Like the above interfaces, NodeLists are also iterable via ECMAScript for-of loops.

From https://chromium.googlesource.com/chromium/src/+/2f9367a3f1bc731e1cae19edfa3b89bc18071093

So you would be correct if you're using Chrome version 50 or older.

troido ยท 16 points ยท Posted at 11:49:02 on June 12, 2016 ยท (Permalink)*

Doesn't work in firefox either.

Here is the same code with a normal for loop that should work in more browsers:

javascript:(function(){style=document.createElement("style");style.innerHTML=".my-spin%20{transition:%201s%20linear;transform:%20rotate(360deg);}";document.getElementsByTagName("head")[0].appendChild(style);function%20toggleSpin(){var%20tables=document.querySelectorAll("table");%20for%20(var%20i=0;i<tables.length;%20i++){var%20a=tables[i];a.classList.toggle("my-spin")}}toggleSpin();setTimeout(toggleSpin,2E3);})();
TarMil ยท 5 points ยท Posted at 18:17:42 on June 12, 2016 ยท (Permalink)

Using Array.prototype.forEach.call() should also work everywhere.

idle_zealot ยท 1 points ยท Posted at 16:10:52 on June 12, 2016 ยท (Permalink)

Ah, I only tested in Chromium, thanks for posting a more compatible option.

dbbeginner ยท 1 points ยท Posted at 18:30:34 on June 12, 2016 ยท (Permalink)

works like a charm in Safari

dbbeginner ยท 1 points ยท Posted at 18:34:30 on June 12, 2016 ยท (Permalink)

Woohoo! lookit me!

javascript:(function(){style=document.createElement("style");style.innerHTML=".my-spin%20{transition:%201s%20linear;transform:%20rotate(360deg);}";document.getElementsByTagName("head")[0].appendChild(style);function%20toggleSpin(){var%20tables=document.querySelectorAll("a");%20for%20(var%20i=0;i<tables.length;%20i++){var%20a=tables[i];a.classList.toggle("my-spin")}}toggleSpin();setTimeout(toggleSpin,2E3);})();

or even worse...

javascript:(function(){style=document.createElement("style");style.innerHTML=".my-spin%20{transition:%201s%20linear;transform:%20rotate(360deg);}";document.getElementsByTagName("head")[0].appendChild(style);function%20toggleSpin(){var%20tables=document.querySelectorAll("div");%20for%20(var%20i=0;i<tables.length;%20i++){var%20a=tables[i];a.classList.toggle("my-spin")}}toggleSpin();setTimeout(toggleSpin,2E3);})();
blueblur112198 ยท 4 points ยท Posted at 16:43:43 on June 13, 2016 ยท (Permalink)

Javascript

That shouldn't work.

What did you expect?

Relevant_Monstrosity ยท 1 points ยท Posted at 22:43:36 on June 12, 2016 ยท (Permalink)

I can fix that for you (this works).

var nodeList = document.querySelectorAll("table");
nodeList.forEach = Array.prototype.forEach;
nodeList.forEach(function(node){
  // do something
});
[deleted] ยท -3 points ยท Posted at 07:24:03 on June 12, 2016 ยท (Permalink)

[deleted]

Scorpius289 ยท -1 points ยท Posted at 07:41:02 on June 12, 2016 ยท (Permalink)

Can confirm that it works.

coladict ยท 2 points ยท Posted at 16:57:32 on June 13, 2016 ยท (Permalink)

I added that emoticon to our site's error page only on error code 500, and the boss didn't object when he saw it. It's just to ease the tension of getting those errors when we have them.

๐ŸŽ™๏ธ jj4th ยท 1 points ยท Posted at 03:57:41 on June 13, 2016 ยท (Permalink)

To you and all the people who posted replies. Good job taking my little hackish lazy-programmer code and improving on it!

[deleted] ยท 0 points ยท Posted at 18:19:47 on June 12, 2016 ยท (Permalink)

[deleted]

idle_zealot ยท 2 points ยท Posted at 18:51:54 on June 12, 2016 ยท (Permalink)

Honestly, I like yours better. Mine adds a new style tag every time it's run, where as yours doesn't leave much behind. Here is your version, but minified:

!function(){for(var t=document.getElementsByTagName("table"),e=0;e<t.length;e++)t[e].style.transform="rotate(360deg)",t[e].style.transition="1s";setTimeout(function(){for(var e=0;e<t.length;e++)t[e].style.transform="rotate(0deg)",t[e].style.transition="initial"},1e3)}();
[deleted] ยท 37 points ยท Posted at 21:49:03 on June 11, 2016 ยท (Permalink)*

[deleted]

ProgramTheWorld ยท 6 points ยท Posted at 23:22:39 on June 11, 2016 ยท (Permalink)

I mean you just have to add an animation in CSS for all table... not much there :)

8bitslime ยท 15 points ยท Posted at 02:01:27 on June 12, 2016 ยท (Permalink)

Didn't know you could run CSS from a bookmark...

nictytan ยท 16 points ยท Posted at 02:03:32 on June 12, 2016 ยท (Permalink)*

You can't. But you can run JavaScript, which can alter CSS.

[deleted] ยท 17 points ยท Posted at 07:30:33 on June 12, 2016 ยท (Permalink)

You can't. But you can run JavaScript, which can alter CSS.

ftfy

nictytan ยท 2 points ยท Posted at 13:25:53 on June 12, 2016 ยท (Permalink)

Whoops, thanks.

ProgramTheWorld ยท 3 points ยท Posted at 02:29:44 on June 12, 2016 ยท (Permalink)

You can by adding a style tag.

ping_less ยท 9 points ยท Posted at 09:01:14 on June 12, 2016 ยท (Permalink)

Oh, how the tables turn...

GetOutOfJailFreeTard ยท 1 points ยท Posted at 14:22:50 on June 12, 2016 ยท (Permalink)

*How the turn tables...

gifv-bot ยท 7 points ยท Posted at 20:58:40 on June 11, 2016 ยท (Permalink)

GIFV link


I am a bot. FAQ // code

yammajr ยท 13 points ยท Posted at 05:07:32 on June 12, 2016 ยท (Permalink)

I'm not sure what's wrong with using tables. When you have tabular data, it totally makes sense to put it in a gasp table.

The problem comes from using tables for page layout. That's a crime and should be treated severely.

Amusing, none the less.

Laugarhraun ยท 4 points ยท Posted at 12:44:44 on June 13, 2016 ยท (Permalink)*

TBH there was a time (10+ years ago?) when it was the best way to get the style you wanted, and its rendering was pretty consistent.

o11c ยท 1 points ยท Posted at 02:00:28 on June 14, 2016 ยท (Permalink)

To be fair, the whole "HTML is semantic" thing isn't really true.

TheDitonation ยท 7 points ยท Posted at 21:34:02 on June 11, 2016 ยท (Permalink)

... But how?

S0PH0S ยท 6 points ยท Posted at 21:49:19 on June 11, 2016 ยท (Permalink)

Can we have the JS code?

๐ŸŽ™๏ธ jj4th ยท 25 points ยท Posted at 22:02:28 on June 11, 2016 ยท (Permalink)*

The code is embarrassingly hackish. I'm too lazy to add the css rules myself, so I just import font-awesome from a CDN. Oh and of course, the bookmarklet is named: (โ•ฏยฐโ–กยฐ)โ•ฏ๏ธต โ”ปโ”โ”ป

javascript:link=document.createElement('link');link.rel='stylesheet';link.type='text/css';link.href='https://goo.gl/STSGxp';document.getElementsByTagName('head')[0].appendChild(link);function toggleSpin(){for(var elem of document.querySelectorAll('table')){elem.classList.toggle('fa-spin')}}toggleSpin();setTimeout(toggleSpin,2000);
caagr98 ยท 17 points ยท Posted at 22:23:39 on June 11, 2016 ยท (Permalink)

If anyone wants a table to test with, here you go.

Foo Bar text text
Foo Bar text text
text text text text
text text text text
text text text text
text text text text
text text text text
hmmwhatsthisdo ยท 17 points ยท Posted at 01:40:21 on June 12, 2016 ยท (Permalink)

link.href='https://goo.gl/STSGxp'

Sneaky way of tracking that, I must say.

[deleted] ยท 2 points ยท Posted at 09:23:03 on June 12, 2016 ยท (Permalink)

[deleted]

[deleted] ยท 3 points ยท Posted at 09:37:46 on June 12, 2016 ยท (Permalink)

https://goo.gl tracks clicks. It's more likely just for the shorter url though.

๐ŸŽ™๏ธ jj4th ยท 1 points ยท Posted at 03:55:25 on June 13, 2016 ยท (Permalink)

That's exactly why, I don't like long URLs. I didn't even realize it tracked clicks.

Howzieky ยท 1 points ยท Posted at 01:27:45 on June 12, 2016 ยท (Permalink)

How does one implement this?

borick ยท 2 points ยท Posted at 01:49:26 on June 12, 2016 ยท (Permalink)

You can just paste it in your URL with this page open, javascript: has to be at the very front.

youwillnevercatme ยท 3 points ยท Posted at 13:07:03 on June 12, 2016 ยท (Permalink)

ELI5: Why everyone hates tables?

lindell92 ยท 9 points ยท Posted at 15:36:19 on June 12, 2016 ยท (Permalink)

Tables are not bad. But using tables to create a layout in HTML is. If you do that you are locked into into having a specific structure of the website and creating things like a responsive website is really hard.

But when presenting data (like in the W3C examples in the image) it is a good tool

[deleted] ยท 3 points ยท Posted at 21:18:48 on June 12, 2016 ยท (Permalink)
jaxklax ยท 1 points ยท Posted at 01:10:54 on June 12, 2016 ยท (Permalink)
rasori ยท 1 points ยท Posted at 04:07:41 on June 12, 2016 ยท (Permalink)

I don't know what's worse, that I like this or that I actually see places where it could be useful in my day job...

wooboy ยท 1 points ยท Posted at 21:58:39 on June 12, 2016 ยท (Permalink)

As much as I don't like the incorrect usage of tables in html, I absolutely detest using html Select elements. They're basically impossible to style without using JavaScript to make a faux drop down and hiding the real Select element.

Relevant_Monstrosity ยท 1 points ยท Posted at 22:46:09 on June 12, 2016 ยท (Permalink)
supnul ยท 1 points ยท Posted at 18:34:01 on June 16, 2016 ยท (Permalink)

Y U NO DIV/SPAN ?

isistitties ยท 0 points ยท Posted at 12:11:48 on June 12, 2016 ยท (Permalink)

w3schools