- This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
Viewing 5 posts - 1 through 5 (of 5 total)
- The forum ‘JavaScript’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › JavaScript › What html element to use for buttons calling actions?
As far as I can tell the standard for this is an <a>
tag, but it doesn’t really make much sense. Especially if you have nothing to put in the ‘href’ attribute so you end up making it ‘#’ or ‘javascript:void’ or something silly like that.
To me the HTML button element kinda makes the most sense, though it can be a bit weird with styling and the fact that you have to specify type="button"
to keep it from submitting a form is odd (and kinda comical.. button type button is not the default?)
What do you guys like to use, and what do you think about the semantics?
Use <button>
it’s much friendlier on mobile
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button
Button is the appropriate element. If you don’t want to specify button, you can in the script that gets called when you press it use event.preventDefault()
Wouldn’t you know it, Chris posted today….
you have to specify type=”button” to keep it from submitting a form …
Note: only if the button is inside a form.
Dunno if that’s relevant to your use case, but I typically don’t have buttons in forms that aren’t for submitting the form.