Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button vs. Clickable Div in HTML

In HTML is it better to use a button or a clickable div?
Either way I'm going to use jQuery to handle the event of the click, but I'm wondering if there are advantages/disadvantages to using one over the other. Such as execution speed, loading speed, etc.

<button id="temp">Click</button>  

vs.

<div id="temp">Click</div>  

The code I'm using to handle the click event looks like this,

$("#temp").click(function(){
//Event details here
};

I understand that the button element is already pre-styled to look like a button and that (on windows) if you you press the enter key after a button has been selected (via click) the button will reactivate as if it were clicked again, but that is both of those factors aren't very important to me.

like image 347
SubliminalNexus Avatar asked Jun 29 '15 11:06

SubliminalNexus


People also ask

Should I use a button or a div?

You should use <a> or <button> , not <div> . an anchor with an href attribute will trigger page reload. Without an href, it is not "tabable", and it still does not carry the button semantic. <a role="button" tabindex="0"></a> does all that.

Can a div act as a button?

All Elements Can Be Buttons Any div, header, footer or other containers can be a w3-button! Any div, header, footer or other containers can be a w3-btn!

Should a div be clickable?

A div is a content division element and is not clickable by default. We may have to use the onclick event handler to process the events emitted by the element when a user clicks on it.

Why button is not clickable in HTML?

A disabled button is unusable and un-clickable. The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the button clickable again.


2 Answers

<button> has some inbuilt things that you may like, but also has some styling restrictions that not apply to <div>.

<button> can be focused, where <div> has to have tabindex attribute to make focus work. Pressing it can submit a form if configurated for ex. has built in onclick handler: <button onclick="submit()">

Usage of <button> applies to HTML5 document specification which puts divs in place of containers elevating role of contextual elements like <footer>, <section> and <button>.

In short I always use <div>s as can style it how I need and program them how I like, am not fancying predefined behaviours as they can vary on different browsers :)

like image 105
Szymon Toda Avatar answered Sep 29 '22 13:09

Szymon Toda


Don't use <div> tags to make clickable elements. Use <a> or <button> elements. This enables browsers with JavaScript disabled to interact with them as expected. Even if your functionality requires JavaScript and there is no reasonable default behaviour you can assign to an <a>, use it regardless - it conveys "clickable" semantics.

In general, choose the tag that most closely describes the function of its content, not the appearance of its content, and avoid unnecessary <div> tags, lest your documents suffer from divitis.

http://csscreator.com/divitis

like image 25
Rajneesh Kumar Gobin Avatar answered Sep 29 '22 14:09

Rajneesh Kumar Gobin