What does it mean when the form action attribute is “#” (number/pound symbol/sign/character)?

StackOverflow https://stackoverflow.com/questions/8862062

  •  16-04-2021
  •  | 
  •  

Question

What does it mean when the form action attribute is "#" (number/pound symbol/sign/character)?

What happens when a form input's formaction attribute is set to "#"? Does this prevent the input from being submitted to the server?

<form method="GET" action="example.php">
    <input type="text" size="20" name="text1" value="text1" formaction="#"/>
    <input type="text" size="20" name="text2" value="text2"/>
    <input type="submit" value="Submit"/>
</form>
Was it helpful?

Solution

The meaning of # as a URL reference (whether as action or formaction attribute value or otherwise) is a reference to the start of the current base document. The base document is the current document, unless a <base href=...> tag has been set.

What happens depends on the situation. Typically, the browser requests for the page again with a query part in the URL (and the page is loaded again, which may imply that client-side scripts are run), but if the same query had been used earlier, the browser probably uses its cache. Moreover, as the start of the document is referred to, focus on any form element is lost and the page may scroll backwards.

So although # is fairly common in some coding styles, it is not reliable; its purpose is better achieved using client-side event handlers.

The formaction attribute has a meaning only for submit buttons. A text input element does not constitute a submit button, even though it may trigger form submission, so here the attribute is ignored.

OTHER TIPS

The form will submit to itself (current URL). I think it is the same as empty action.

Also, can be useful if action is going to be changed by javascript at later time.

Explained on w3schools: http://www.w3schools.com/html5/html5_form_attributes.asp

Form Override Attributes

The form override attributes allow you to override some of the attributes set for the form element.

The form override attributes are:

formaction - Overrides the form action attribute
formenctype - Overrides the form enctype attribute
formmethod - Overrides the form method attribute
formnovalidate - Overrides the form novalidate attribute
formtarget - Overrides the form target attribute
Note: The form override attributes works with the following types:
submit and image.

<form action="demo_form.asp" method="get" id="user_form">
E-mail: <input type="email" name="userid" /><br />
<input type="submit" value="Submit" />
<br />
<input type="submit" formaction="demo_admin.asp" value="Submit as admin" />
<br />
<input type="submit" formnovalidate="true"
value="Submit without validation" />
<br />
</form>

So yes, you are absolutely correct that it overrides the action, but it only overrides on input type submit and image, not on text. So you can have 2 different submitbuttons in the same form but can lead to different types of validaton. That's what I would use it for.

So a # would put the action on the same page rather than another one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top