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>
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.
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.
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.
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 .
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.
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With