Hacker News new | past | comments | ask | show | jobs | submit login

  <button onclick="window.location.href='https://example.com';">I'm a button with a link</button>

This code from the demo shows, why not using classes to style a website is a misguided idea.

Maybe something like this would be a better idea if you absolutely must avoid classes:

https://jsfiddle.net/qa2cdj4v/

  <strong><a href="#">Button</a></strong>

  strong > a:first-child:last-child {
    display: inline-block;
    background: red;
    color: white;
    padding: 10px;
    font-weight: normal;
  }



I can't understand why so many websites use an onclick handler on a button to redirect to websites. It's not hard to even find something like <a href="#" onclick="location.href='https://example.com';">click here</a>

The worst part is that it's not just random websites doing this, this can be found on most FAANG websites as well.


why not just use

  <form action="https://example.com"><button type="submit">Link button</button></form>
no need for JS


Because you cannot make `form` `inline-block` for only this instance without classes.


Why not use use an anchor tag?


Yep that's a pretty fundamental accessibility failure.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: