News for hackers. Stuff that matters.
Re:HTML is the Web by dccoolgai on Wednesday June 26, 2019 @11:02AM (#20285662)
I think what the author is saying is that paradigm 2 is built _on top of_ paradigm 1 and if you can't do #1 right, then by definition _neither_ can be right. It's astounding the number of interview candidates who are super-fluent in JS but can't tell you to save their lives why it's a bad idea to put a click handler on a div. React will die (It's already started to) and we'll migrate back to the actual Web at some point and for my money it can't happen fast enough.
Re:HTML is the Web by hombre_fatal on Wednesday June 26, 2019 @11:23AM (#20285888)
> It's astounding the number of interview candidates who are super-fluent in JS but can't tell you to save their lives why it's a bad idea to put a click handler on a div.

Go on?

Re:HTML is the Web by paulhebert on Wednesday June 26, 2019 @11:59AM (#20286218)
Using a button here would be more appropriate for a few reasons:

Buttons by default have keyboard support for focusing and clicking. You’d have to manually add this to a div.

Screenreaders, scrapers, and other automated tools reading your html understand buttons and treat them differently from divs.

It’s easier for other devs to understand your code at a glance if clickable elements are buttons.

Re:HTML is the Web by snek on Wednesday June 26, 2019 @11:56AM (#20286191)
Putting a click element on a div is transparent to accessibility systems. And no, it isn't possible to consider an element with an onclick the same as a button, because events bubble, which means the element with the event listener isn't necessarily the element being watched for a click. IIRC there even used to be a "good practice" where people would attach a single click handler at the root and use it to dispatch events based on the `event.target` instead of attaching a separate handler to each target.
Re:HTML is the Web by dsego on Wednesday June 26, 2019 @07:40PM (#20290451)
React does that, they call it synthetic events, but it's hidden from the developer. In jQuery it used to be called event delegation, made it easy to handle events on dynamic elements without the need to constantly attach/detach handlers.
Re:HTML is the Web by dreamcompiler on Wednesday June 26, 2019 @11:58AM (#20286211)
Because the user can't hover over it and see where the link goes. Because the click handler represents a Javascript program that has to be downloaded. Because it can't be used with Javascript turned off -- which is increasingly necessary these days since ad networks abuse Javascript.

But mostly because it's usually unnecessary and smacks of laziness on the developer's part.

Re:HTML is the Web by saagarjha on Wednesday June 26, 2019 @11:51AM (#20286134)
Because you've literally just described what a <button> is.
Re:HTML is the Web by felxh on Wednesday June 26, 2019 @01:49PM (#20287338)
That would mean that anything that responds to click events is a button. From your experience of using UIs, you can probably think of many more UI elements that respond to clicks and are not buttons
Re:HTML is the Web by frenchyatwork on Wednesday June 26, 2019 @11:12AM (#20285776)
> React will die

Hopefully, but people have a large capacity to deal with broken web software. Back when I was young, it was all the Java & .Net websites that turned every friggin link into a POST form so that navigating back, and opening in a new window was completely broken.

Re:HTML is the Web by antibland on Wednesday June 26, 2019 @12:55PM (#20286800)
> React will die (It's already started to)

I read a lot of articles about front end, including framework & library trends, usage statistics, rumors, etc., though I've never heard that React has begun to die. Can you provide a citation?

Re:HTML is the Web by robertoandred on Wednesday June 26, 2019 @12:52PM (#20286768)
Nothing about React stops you from correctly putting click handlers on buttons.
Re:HTML is the Web by tracker1 on Wednesday June 26, 2019 @06:06PM (#20289734)
Well, the behavior is a bit awkward, and older browsers don't even work right. Of course, some older browsers that shall not be named, you couldn't disable certain styling attributes of buttons, which means an anchor may have been more appropriate back then.

I tend to prefer semantic tags most of the time. I do wish there was a CSS declaration, or maybe an <html> attribute to start off with absolutely NO styling, so that it all comes up from what's in the CSS only. Various resets suck or don't work well, or are really big for various reasons. For that matter, being able to reset everything under a given element that way so that in-browser rules aren't applied would be really nice for WebComponents.

My own weakest point is probably on CSS these days, I just haven't really kept up since 2.x as I haven't really needed to, and can usually google when I need more. I feel the flexbox stuff is more complex than it should be.

Re:HTML is the Web by stellar678 on Wednesday June 26, 2019 @11:27AM (#20285932)
I’ll bite: Why is it bad to put a click handler on a div? Because we’re losing the semantic content of the markup?
Re:HTML is the Web by citrablue on Wednesday June 26, 2019 @12:34PM (#20286583)
Because there's already an element for that. Putting a click handler on a div is a hack.

In a clean (from scratch) dev situation, when would it make sense to use the div option?

Re:HTML is the Web by mattwad on Wednesday June 26, 2019 @03:47PM (#20288446)
People have been putting event listeners on div long before any framework was around. Tons of libraries and documentation show how to attach a click handler to a div (ex: https://api.jquery.com/click/). I can think of some cases... maybe the div is contenteditable and I want to trigger some analytics. Maybe I want to listen to all the children of a container rather than apply expensive listeners all over. Do you put button tags around all your tooltips? You might end up making your HTML look right, but now you are jumping through hoops to make it not look like a button. As with all things, it's naive to say "never do something."
Re:HTML is the Web by cortesoft on Wednesday June 26, 2019 @11:56AM (#20286195)
For one, it won't play nice with accessibility helpers... screen readers, keyboard interaction, etc.
Re:HTML is the Web by giobox on Wednesday June 26, 2019 @12:17PM (#20286373)
While I agree semantically use of <button> can be clearer, clickable divs can still play largely fine with most accessibility technologies if ARIA attributes are used.

Does it semantically make the most sense? Not necessarily, but it isn't a blocker to most accessibility tools and use and support for ARIA attributes is pretty widespread nowadays, including all of the major browsers and screen readers.

> https://developers.google.com/web/fundamentals/accessibility...

> https://developer.mozilla.org/en-US/docs/Web/Accessibility/A...

Re:HTML is the Web by ricardobeat on Wednesday June 26, 2019 @12:46PM (#20286709)
Number of times I remember seeing an an app using `<div onclick>` with proper aria attributes: 0.
Re:HTML is the Web by giobox on Wednesday June 26, 2019 @01:11PM (#20286959)
It's often a legal or contractual requirement when selling software to public bodies in many countries, so I've seen it used a lot. Mileage may vary in other markets.

The important point is that fundamentally using a <div> in place of <button> can still be made to work easily with these accessibility tools - many comments here imply it can't be done at all and only a button will work. Does it semantically make sense? Likely no, but it isn't a hard blocker on accessibility.

Re:HTML is the Web by mosdl on Wednesday June 26, 2019 @01:23PM (#20287065)
Reinventing the wheel is bad