Which HTML tags can be used with onClick?

11,698

All HTML elements can have an onclick attribute.

See the HTML 5 specification for confirmation.

(Of course, some elements are not rendered by default so you would have to alter their display properties with CSS before they can be clicked on to trigger the event handler).

Share:
11,698

Related videos on Youtube

Mario Boss
Author by

Mario Boss

const describeMe = prompt('How you can describe me?'); const regex = /not|BAD|AS{1,2}|for|a|dev+/igm; alert('Your description is ' + regex.test(describeMe));

Updated on June 05, 2022

Comments

  • Mario Boss
    Mario Boss almost 2 years

    Which HTML tags can have an onclick event and which cannot? If there are tags where I cannot include an onclick event I want to know why not?

    onclick="func()"
    

    Are there any rules that you should adhere to?

    • Laurent S.
      Laurent S. about 10 years
      as a side remark, you should avoid inline javascript...
  • pes502
    pes502 about 10 years
    I think, that he talking about HTML tags, not about element classes or id's
  • Admin
    Admin about 10 years
    to include an onClick in a html or title tag is nonsensical, but i can do this?
  • TylerH
    TylerH about 10 years
    Yes you can do this, but it is not recommended as it is obtrusive.
  • Quentin
    Quentin about 10 years
    It isn't nonsensical. The html element is rendered, so you could bind an event handler to it to trigger if any part of the document is clicked (quite useful if you want to generate heat maps of your page, although I'd tend to bind directly on the window object myself). It is rather odd to make a title clickable, but head, title { display: block; } in a stylesheet will make it possible.
  • Damon
    Damon about 10 years
    I would strongly advise not mixing your js within your html by adding an "onClick" attribute to your html whenever possible. Linking to an external js file - that hooks into your markup is the ideal way to roll. The above is how you would do that.
  • Damon
    Damon about 10 years
    EDIT: I updated the js to show how you can talk directly to the element. Be sure your element in question has loaded & is available before talking to it. Otherwise, nothing will happen. Use these calls within a/an onready statement (or at the bottom of your html).
  • Daniel Bragg
    Daniel Bragg about 5 years
    @TylerH, so what elements are there for which this is recommended and non-obtrusive?
  • TylerH
    TylerH about 5 years
    @Dan Most others below <body>, frankly.
  • Daniel Bragg
    Daniel Bragg about 5 years
    @TylerH "below <body>"? Do you mean "most elements that can appear within the <body> tag, except for <div> and <span>", or elements that are listed somewhere in a list where "body" is in the middle of the list?
  • TylerH
    TylerH about 5 years
    @Dan I mean the former.