Sorry if this seems redundant. I have read through quite a few posts and cannot seem to figure out how to accomplish this. I need an element that behaves like a button but I thought to use a css class object instead. I have found a number of references showing how to do this by making href="#" and adding javascript command.
<p><input name="Test" type="Button" value="Click" /></p>
However, due to the server side software I am using I need to exactly duplicate the behavior of the button in order to make it submit my form.
I am guessing it is something obvious like:
<p><a class="button" href="#" onclick="submit()">Click</a></p>
Thanks for any help you can provide!
You have to reference to the form element, and then call the submit:
<p><a class="button" href="#" onclick="document.getElementById('myFormId').submit()">Click</a></p>
Though you should not be using inline event delegation:
HTML:
<p><a class="button" href="#" id="submit-anchor">Click</a></p>
JS:
document.getElementById("submit-anchor").addEventListener("click", function() {
document.getElementById('myFormId').submit();
});
This will do the job:
<a class="button" href="#" onclick="document.forms[0].submit();">Click</a>
If you have one form in the document, else change 0 accordingly.
OR
<a class="button" href="#" onclick="document.forms['formName'].submit();">Click</a>
Uses form name.
OR
<a class="button" href="#" onclick="document.getElementById('formID').submit();">Click</a>
Uses form id.
you have to use "form.submit();"
you should check out this question: Use a normal link to submit a form
Please help,
I am getting error via http://validator.w3.org/ while validating my html5 template. I get the message "The element button must not appear as a descendant of the a element.". I can't understand this. Could anyone help me to solve this?
Here is my code:
<div class="post-response btn-group btn-group-lg">
<button type="button" class="btn btn-default"><i class="fa fa-comment"> 5 Comments</i></button>
<button type="button" class="btn btn-default"><i class="fa fa-heart"> 5 Likes</i></button>
</div>
The usual error from the W3C validator is:
The element button must not appear as a descendant of the a element
This makes more sense for your question, as you have a button that is a descendant of an anchor.
Normally, you could solve the issue by simply styling your anchor tag, or by placing the button within a form element:
<form style="display: inline" action="http://www.example.com/" method="get">
<button>Some Call to Action</button>
</form>
But in your case, I don't think you even need the anchors as you aren't linking anywhere using HTML (and if you are attaching events using JavaScript, you could simply attach those events to the button rather than the anchor.
I have a piece of code in my flask application that I want to implement a button that takes me to an specific page.
<button type="button" class="btn btn-primary btn-block">Evaluate</button>
How do I link the button? I did putting an tag wrapping it, but can I use the tag directly? (Using the tag is changing my styling)
*I did the code below, but it is not good for me
<button type="button" class="btn btn-primary btn-block">Evaluate</button>
Use <a> tag instead
Evaluate
use the href attribute to navigate to the new location.
Bootstrap will take care of presenting the <a> tag as a button with the css classes.
My application uses "button" html elements for submitting forms. The code is below...
<button class="button save" name="save" type="submit">Apply Changes</button>
However, I'm finding that sometimes I need to click the button a few times in order to get it to submit. Is this a known issue with the html button object?
Can I apply an onclick handler to insure the button fires consistently on first click?
Browsers will react differently to the <button> tag, especially Internet Explorer. If you are using this button for an HTML form, <input type="submit"> is the way to go since it's fully supported by major browsers.
Both <button> and <input> tags can provide a rich content if CSS attributes are done correctly, despite what you read on the internet. I always go with <input>, it's safer.
I am doing the following:
<a href="www.stackoverflow.com">
<button disabled="disabled" >ABC</button>
</a>
This works good but I get a HTML5 validation error that says "Element 'button' must not be nested within element 'a button'.
Can anyone give me advice on what I should do?
No, it isn't valid HTML5 according to the HTML5 Spec Document from W3C:
Content model: Transparent, but there must be no interactive content descendant.
The a element may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links).
In other words, you can nest any elements inside an <a> except the following:
<a>
<audio> (if the controls attribute is present)
<button>
<details>
<embed>
<iframe>
<img> (if the usemap attribute is present)
<input> (if the type attribute is not in the hidden state)
<keygen>
<label>
<menu> (if the type attribute is in the toolbar state)
<object> (if the usemap attribute is present)
<select>
<textarea>
<video> (if the controls attribute is present)
If you are trying to have a button that links to somewhere, wrap that button inside a <form> tag as such:
<form style="display: inline" action="http://example.com/" method="get">
<button>Visit Website</button>
</form>
However, if your <button> tag is styled using CSS and doesn't look like the system's widget... Do yourself a favor, create a new class for your <a> tag and style it the same way.
If you're using Bootstrap 3, this works quite well
Primary link
Link
I've just jumped into the same issue and I solved it substituting 'button' tag to 'span' tag. In my case I'm using bootstrap. This is how it looks like:
<a href="#register">
<span class="btn btn-default btn-lg">
Subscribe
</span>
</a>
No.
The following solution relies on JavaScript.
<button type="button" onclick="location.href='http://www.stackoverflow.com'">ABC</button>
If the button is to be placed inside an existing <form> with method="post", then ensure the button has the attribute type="button" otherwise the button will submit the POST operation. In this way you can have a <form> that contains a mixture of GET and POST operation buttons.
It would be really weird if that was valid, and I would expect it to be invalid. What should it mean to have one clickable element inside of another clickable element? Which is it -- a button, or a link?
These days even if the spec doesn't allow it, it "seems" to still work to embed the button within a <a href...><button ...></a> tag, FWIW...
Another option is to use the onclick attribute of the button:
<button disabled="disabled" onClick="location.href='www.stackoverflow.com'" >ABC</button>
This works, however, the user won't see the link displayed on hover as they would if it were inside the element.
You can add a class to the button and put some script redirecting it.
I do it this way:
<button class='buttonClass'>button name</button>
<script>
$(".buttonClass').click(function(){
window.location.href = "http://stackoverflow.com";
});
</script>
why not..you can also embeded picture on button as well
<FORM method = "POST" action = "https://stackoverflow.com">
<button type="submit" name="Submit">
<img src="img/Att_hack.png" alt="Text">
</button>
</FORM>
Explanation and working solution here:
Howto: div with onclick inside another div with onclick javascript
by executing this script in your inner click handler:
if (!e) var e = window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
It is illegal in HTML5 to embed a button element inside a link.
Better to use CSS on the default and :active (pressed) states:
body{background-color:#F0F0F0} /* JUST TO MAKE THE BORDER STAND OUT */
a.Button{padding:.1em .4em;color:#0000D0;background-color:#E0E0E0;font:normal 80% sans-serif;font-weight:700;border:2px #606060 solid;text-decoration:none}
a.Button:not(:active){border-left-color:#FFFFFF;border-top-color:#FFFFFF}
a.Button:active{border-right-color:#FFFFFF;border-bottom-color:#FFFFFF}
<p><a class="Button" href="www.stackoverflow.com">Click me<a>
Use formaction attribute inside the button
PS! It only works if your button type="submit"
<button type="submit" formaction="www.youraddress.com">Submit</button>