button tag submitting - html

i am using this plain html code inside an aspx page. it renders well, but when clicked it submits / reloads the page. i dont want anything to be done on click of this button. whats d issue
<button>
btn1</button>
<button>
btn2</button>

It'll default to type=submit if no type is explicitly given.
You want to put
<button type="button">btn1</button>

Not sure if by pure html you can do so however this way you can black the default submit behaviour:
<button id="a" onclick="return false;">button</button>

Don't use <button>. Use <input> instead:
<input type="button" id="btn1" />
Also, worth noting (from http://www.w3schools.com/tags/tag_button.asp):
If you use the button element in an
HTML form, different browsers will
submit different values. Internet
Explorer will submit the text between
the <button> and </button> tags, while
other browsers will submit the content
of the value attribute. Use the input
element to create buttons in an HTML
form.

Related

Using Input Type Submit Or Button

I have to use a submit button to submit one of my forms on a website. Is it possible to use the <button> element as a submit button? Do I also add the type="submit" attribute to the <button> element? i.e.
<button type="submit">Go</button>
Yes, it's possible!
In fact, the <button>'s default behavior is to act as a submit button, so:
<button>Go</button>
Will do what you expect it to! Buttons are also slightly easier to style, and you can keep them consistent with other types of buttons without adding extra rules or selectors in your CSS.
It's worth noting that IE 7 and 6 have some pretty horrible bugs when using <button value="whatever">Something Else</button>. If you want lower IE compatibility and to use the value= attribute, don't use <button>
Following are also 2 types to submit forms
<input type="submit">
The following script
<script>
function SubmitFunction()
{
document.getElementById("formid").submit();
}
</script>
<form id="formid" action="abc.php">
<input type="button" onclick="SubmitFunction()" value="Submit">
</form>
yes
You can use but I have heard that If you use the <button> element in an HTML form,
different browsers may submit different values. Use <input> to create buttons in an HTML form.

code for submit button works in ff but not in ie

the following code works in FF, but it does not work in IE8.
<a class="sub">
<input type="submit" a="" none;<="" display:="">
</a>
The button is displayed, but the button is not clickable in IE8. What is going on?
Here is a correctly formed input submit button:
<input type="submit" value="Submit">
I noticed the words display and none usually you'd find it in the following form:
<input type="submit" value="Submit" style="display:none;"> // this however will hide the button
The attribute of type= with the value of "submit" makes our input tag into a submit button.
The attribute of value= with the value of "Submit" displays "Submit" text on our input button.
The attribute of style= allows us to do some inline css like "display:none;" which hides a html element its declared on.
I recommend checking out W3Schools for more on html input tags.
Additionally you are trying to make the button into a link using the <a> tag, this is invalid, please take a look at this Html forms and input page to see how to use the submit input type.
If you just want a link then I'd recommend looking at an <img> for a button and an <a> tag around that.
I do not have comment privileges on this SE yet but I would say that IE is incapable if interpreting: <input type="submit" a="" none;<="" display:="">
When you open "input" you do not close it until after display. the addition of the "none;<" is probably interpreted as another attempt at a tag and breaking html. I'm not sure what you are trying to accomplish but get rid of this:
none;<=""
The code is a mess, but it actually works in IE8, so any problem you are seeing is caused by something outside the code you posted.
Browsers will ignore the crap inside the input tag and recognize it simply as <input type="submit">. Some browsers might conceivably choke on the “<” character there, but IE8 doesn’t.
The a element wrapper outside it has no effect as such.

Using a <button type="submit">Save</button> does not fire consistenttly

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.

Difference between <input type='submit' /> and <button type='submit'>text</button>

There are many legends about them. I want to know the truth. What are the differences between the two following examples?
<input type='submit' value='text' />
<button type='submit'>text</button>
Not sure where you get your legends from but:
Submit button with <button>
As with:
<button type="submit">(html content)</button>
IE6 will submit all text for this button between the tags, other browsers will only submit the value. Using <button> gives you more layout freedom over the design of the button. In all its intents and purposes, it seemed excellent at first, but various browser quirks make it hard to use at times.
In your example, IE6 will send text to the server, while most other browsers will send nothing. To make it cross-browser compatible, use <button type="submit" value="text">text</button>. Better yet: don't use the value, because if you add HTML it becomes rather tricky what is received on server side. Instead, if you must send an extra value, use a hidden field.
Button with <input>
As with:
<input type="button" />
By default, this does next to nothing. It will not even submit your form. You can only place text on the button and give it a size and a border by means of CSS. Its original (and current) intent was to execute a script without the need to submit the form to the server.
Normal submit button with <input>
As with:
<input type="submit" />
Like the former, but actually submits the surrounding form.
Image submit button with <input>
As with:
<input type="image" />
Like the former (submit), it will also submit a form, but you can use any image. This used to be the preferred way to use images as buttons when a form needed submitting. For more control, <button> is now used. This can also be used for server side image maps but that's a rarity these days. When you use the usemap-attribute and (with or without that attribute), the browser will send the mouse-pointer X/Y coordinates to the server (more precisely, the mouse-pointer location inside the button of the moment you click it). If you just ignore these extras, it is nothing more than a submit button disguised as an image.
There are some subtle differences between browsers, but all will submit the value-attribute, except for the <button> tag as explained above.
With <button>, you can use img tags, etc. where text is
<button type='submit'> text -- can be img etc. </button>
with <input> type, you are limited to text
In summary :
<input type="submit">
<button type="submit"> Submit </button>
Both by default will visually draw a button that performs the same action (submit the form).
However, it is recommended to use <button type="submit"> because it has better semantics, better ARIA support and it is easier to style.

How do I make a "div" button submit the form its sitting in?

I have ASP.Net code generating my button's HTML for me using divs to get it to look and behave how I want. This question is regarding the HTML produced by the ASP.Net code.
A standard button is easy, just set the onClick event of the div to change the page location:
<div name="mybutton" id="mybutton" class="customButton" onClick="javascript:document.location.href='wherever.html';">
Button Text
</div>
This works great, however, if I want a button like this to submit the form in which it resides, I would have imagined something like below:
<form action="whatever.html" method="post">
<div name="mysubmitbutton" id="mysubmitbutton" class="customButton" onClick="javascript:this.form.submit();">
Button Text
</div>
</form>
However, that does not work :( Does anyone have any sparkling ideas?
onClick="javascript:this.form.submit();">
this in div onclick don't have attribute form, you may try this.parentNode.submit() or document.forms[0].submit() will do
Also, onClick, should be onclick, some browsers don't work with onClick
Are you aware of <button> elements? <button> elements can be styled just like <div> elements and can have type="submit" so they submit the form without javascript:
<form action="whatever.html" method="post">
<button name="mysubmitbutton" id="mysubmitbutton" type="submit" class="customButton">
Button Text
</button>
</form>
Using a <button> is also more semantic, whereas <div> is very generic. You get the following benefits for free:
JavaScript is not necessary to submit the form
Accessibility tools, e.g. screen readers, will (correctly) treat it as a button and not part of the normal text flow
<button type="submit"> becomes a "default" button, which means the return key will automatically submit the form. You can't do this with a <div>, you'd have to add a separate keydown handler to the <form> element.
There's one (non-) caveat: a <button> can only have phrasing content, though it's unlikely anyone would need any other type of content when using the element to submit a form.
To keep the scripting in one place rather than using onClick in the HTML tag, add the following code to your script block:
$('#id-of-the-button').click(function() {document.forms[0].submit()});
Which assumes you just have the one form on the page.
Why does everyone have to complicate things. Just use jQuery!
<script type="text/javascript">
$(document).ready(function() {
$('#divID').click(function(){
$('#formID').submit();
)};
$('#submitID').hide();
)};
</script>
<form name="whatever" method="post" action="somefile.php" id="formID">
<input type="hidden" name="test" value="somevalue" />
<input type="submit" name="submit" value="Submit" id="submitID" />
</form>
<div id="divID">Click Me to Submit</div>
The div doesn't even have to be in the form to submit it. The only thing that is missing here is the include of jquery.js.
Also, there is a Submit button that is hidden by jQuery, so if a non compatible browser is used, the submit button will show and allow the user to submit the form.
A couple of things to note:
Non-JavaScript enabled clients won't be able to submit your form
The w3c specification does not allow nested forms in HTML - you'll potentially find that the action and method tags are ignored for this form in modern browsers, and that other ASP.NET controls no longer post-back correctly (as their form has been closed).
If you want it to be treated as a proper ASP.NET postback, you can call the methods supplied by the framework, namely __doPostBack(eventTarget, eventArgument):
<div name="mysubmitbutton" id="mysubmitbutton" class="customButton"
onclick="javascript:__doPostBack('<%=mysubmitbutton.ClientID %>', 'MyCustomArgument');">
Button Text
</div>
You have the button tag
http://www.w3schools.com/tags/tag_button.asp
<button>What ever you want</button>