23

I have a header element which needs to fire off some JavaScript when clicked. I know I should only use <a> tags when the page is actually changing, and that <button>s are preferred for JS functions, but for some reason it just feels wrong to do

<h2><button onclick="myFunction();">My Title</button></h2>

I can't put my finger on why that doesn't feel semantically correct. Is it just me?

4
  • 4
    See W3C Validator
    – Oriol
    Commented May 12, 2014 at 21:21
  • 2
    What is the actual text for your button? Would it be appropriate as a heading on it's own?
    – Mog
    Commented May 12, 2014 at 21:22
  • Yeah, the text for the button is the title of the content that's being revealed onclick.
    – suryanaga
    Commented May 12, 2014 at 21:24
  • button isn't preferred for js functions, its for ajax on the page. regardless of how it feels to you, its the proper use....the part about your feelings really makes me think this isn't even a real question.
    – albert
    Commented May 13, 2014 at 9:44

4 Answers 4

24

According to W3C Validator, there's no problem in doing that.

You can try validating this code:

<!DOCTYPE html>
<html>
<head>
   <title>I AM YOUR DOCUMENT TITLE REPLACE ME</title>
</head>
<body>

<h2><button onclick="myFunction();">My Title</button></h2>

</body>
</html>

However, I suggest avoiding inline styles.

7
  • 10
    He's not asking if it's technically valid, he's asking about semantics.
    – Mog
    Commented May 12, 2014 at 21:23
  • 4
    I think @sanjay wants to know if it's considered meaningful in some way, not if it's valid code. Good to know it's valid code, but not really an answer to his question. Commented May 12, 2014 at 21:23
  • 2
    @WesleyMurch Then the question is primarily opinion-based, because there's no objective way to test semantic validity.
    – Oriol
    Commented May 12, 2014 at 21:26
  • 1
    The only relevance here is validity. Otherwise it is the same as "Why put a BUTTON in a DIV?" Commented May 12, 2014 at 21:26
  • 1
    @user2864740 Marking up a list with <div> instead of <li> is valid as well, however it is not semantically correct.
    – Mog
    Commented May 12, 2014 at 21:28
9

What is the actual text for your button? Would it be appropriate as a heading on it's own?

the text for the button is the title of the content that's being revealed onclick

Then I think you're in good shape. I will however make a small suggestion that might make you feel better:

<h2><a href="#content">My Heading</a></h2>
<element id="content"> [your content] </element>

Then attach an onclick handler in an external javascript file. If you remove it later the link will still be valid.

5

HTML5 standard quote that says you can

https://html.spec.whatwg.org/multipage/sections.html#the-h1,-h2,-h3,-h4,-h5,-and-h6-elements 4.3.6 "The h1, h2, h3, h4, h5, and h6 elements" says that those elements can only have phrasing content ("inliny things" like bold or italics or links):

Content model: Phrasing content.

And https://html.spec.whatwg.org/multipage/form-elements.html#the-button-element says that button is phrasing content:

Categories: Flow content. Phrasing content. [yada]

So you can nest them.

0

Yes, but not vise vera. Element h2 not allowed as child of element button.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – KiynL
    Commented Mar 26, 2023 at 11:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.