Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a <button> in HTML do something without javascript?

Tags:

html

button

I have a web page that has a button. Currently I am binding this button to a javascript handler that makes the page redirect to a given URL when it is clicked.

HTML:

<button class="click-me">Clike me to navigate</button>

JS:

$('.click-me').on('click', function() { window.location = '/myloc'; });

I am now trying to build a fallback (no-javascript) version of this page. I would like this button to do the same job but without using javascript. In other words, I am trying to make this button functional even if javascript of the browser is disabled.

I know this can be achieved by form (as shown below). But I am looking for any cleaner implementation.

<form name="hack" action="/myloc">
  <button type="submit" class="click-me">Click me to navigate</button>
</form>
like image 802
Goje87 Avatar asked Nov 12 '12 17:11

Goje87


People also ask

Can HTML work without JavaScript?

If we're talking about websites “working” or not with or without JavaScript, a site that is client-side rendered will 100% fail without JavaScript. It is sort of the opposite of “server-side rendered” ( SSR ) in which the document comes down as HTML right from the server.

Can you use onclick without JavaScript?

The best answer demonstrates a lot of cool tricks that can be done with pure CSS , however, it does not demonstrate how to post data to a form using onclick without JavaScript . Most people will tell you that it is not possible, however that is simply not true.

How do I make a button do something in HTML?

The <button> element is used to create an HTML button. Any text appearing between the opening and closing tags will appear as text on the button. No action takes place by default when a button is clicked. Actions must be added to buttons using JavaScript or by associating the button with a form.

Can you do buttons with HTML?

HTML gives you several ways to add buttons to your website – with the button tag, the anchor link, and the input types of button and submit .


2 Answers

You can simply add implementation of button click (if that references any webpage) by adding an <a> tag before it. For example:

<a href="https://www.youtube.com/">
    <button>Youtube</button>
</a>

This doesn't need any JavaScript as far as I know.

like image 126
Prashant Thapa Avatar answered Nov 15 '22 18:11

Prashant Thapa


Use an a element, that's what they're for:

<a class="click-me" href="/path/to/whatever/page">Click me to navigate</a>

And then simply style it as a button.

For example with the following CSS:

.clickMe {
    -moz-appearance: button;
    -ms-appearance: button;
    -o-appearance: button;
    -webkit-appearance: button;
    appearance: button;
    text-decoration: none;
    color: #000;
    padding: 0.2em 0.4em;
}​

JS Fiddle demo.

(Obviously this assumes the use of a browser that supports the appearance (or vendor-prefixed versions) CSS property.)

References:

  • appearance (albeit it's the -moz- prefixed MDN documentation).
  • appearance property, at the W3C.
  • Unfortunately-dropped CSS3 features (at the W3C).
like image 43
David Thomas Avatar answered Nov 15 '22 17:11

David Thomas