Skip to content
CoursesWeb Forum

Should a clickable thing be a button or a div with onclick

Asked by Noor A. · HTML Structure and Semantics1 replyupdated

Short answer

I am a designer moving into front end code, and I am rebuilding a pricing toggle from a design file. The developer before me wrote it like this:

Written by Noor A. ·
CiteCoursesWeb Forum, "Should a clickable thing be a button or a div with onclick", https://coursesweb.net/forum/t/should-a-clickable-thing-be-a-button-or-a-div-with-onclick/, accessed 2026-11-27.
  1. Member ·
    <div class="toggle" onclick="switchPlan()">Yearly</div>
    

    It looks right and works with a mouse. But when I test with only the keyboard, Tab skips straight past it, and Enter does nothing. Our screen reader test (VoiceOver) reads it as plain text "Yearly" with no hint that it does anything.

    I could add tabindex="0" and a keydown listener, but that feels like rebuilding something that should already exist. Is there ever a good reason to use a div with onclick instead of a real button? And if I switch to <button>, how do I get rid of the default grey browser styling without breaking anything?

    0
    ReplyLink

Plain text or simple Markdown. Links are nofollow. Your email is never shown.

  1. Dov L.Member ·

    In 2011 we made everything a div or an anchor with href="#" and styled it. Nobody tested with a keyboard. So I can tell you why the old code looks like that, not that it was right.

CoursesWeb Forum. Questions and answers on learning web development: HTML, CSS, JavaScript, PHP, MySQL, Node.js and AI coding help.