Is it semantically valid to put a <button> inside a <h2>?

html

Solution

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.

Problem

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?

Original source