HTML dirname Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Forms & Input

What You’ll Learn

The dirname attribute lets a form submit the text direction of user input alongside the field value. Use it on <textarea> and certain <input> types so your server receives both the text and whether it was typed left-to-right (ltr) or right-to-left (rtl). It is not the same as the dir attribute.

01

Form Submit

Extra direction field.

02

Field Name

Not ltr or rtl.

03

textarea

Common use case.

04

text Inputs

Search, email, etc.

05

Pair with dir

Display + submit.

06

dirName JS

Set dynamically.

Purpose of dirname

When users type in multilingual forms, the server often needs to know whether the input is LTR or RTL so it can store, display, or process the text correctly. The dirname attribute solves this by adding a second name/value pair to the form submission.

If you write dirname="comment-dir" on a textarea named comment, the browser submits both comment=… and comment-dir=ltr (or rtl). The browser determines direction from the element’s dir attribute and the text content.

⚠️
Not a direction keyword

dirname="ltr" is incorrect. The attribute value is the name of the direction field (e.g. comment-dir), not the direction itself.

📝 Syntax

Add dirname with a field name on a supported control, and pair it with dir:

dirname.html
<form method="get" action="/submit">

  <textarea

    name="comment"

    dir="auto"

    dirname="comment-dir"></textarea>

  <button type="submit">Send</button>

</form>

Syntax Rules

  • The attribute value is a form field name for the direction (e.g. comment-dir).
  • Only valid on <textarea> and specific <input> types.
  • Pair with dir="auto" (or ltr / rtl) on the same element.
  • On submit, the browser sends {dirname}=ltr or {dirname}=rtl.

💎 Values

The dirname attribute takes a string that becomes a form field name. It does not accept ltr or rtl as the attribute value:

  • dirname="comment-dir" — Submits comment-dir=ltr or comment-dir=rtl.
  • dirname="message-direction" — Submits message-direction=ltr or message-direction=rtl.
  • The submitted direction value (ltr / rtl) is chosen by the browser based on the control’s text directionality.
submitted-data.html
<!-- Markup -->

<input type="text" name="fruit" dirname="fruit-dir" value="cherry">



<!-- Submitted query string -->

fruit=cherry&fruit-dir=ltr

⚡ Quick Reference

ItemDetailsNotes
PurposeSubmit text directionSecond form field
Attribute valueField name stringe.g. comment-dir
Submitted valueltr or rtlSet by browser
Pair withdir on same elementOften dir="auto"
JS propertyelement.dirNamecamelCase in DOM
Not valid onpassword, checkbox, etc.Limited input types

Applicable Elements

ElementSupported?Notes
<textarea>YesMost common use case
input type="text"YesComments, names, messages
input type="search", url, tel, emailYesText-like inputs
input type="hidden"YesRare; direction of hidden value
input type="password", checkbox, etc.Nodirname not supported

dirname vs dir

AttributeValue meansRole
dirltr, rtl, autoControls display direction in the browser
dirnameForm field name (e.g. comment-dir)Submits detected direction to the server

Use both together: dir for rendering, dirname so the backend receives direction metadata on form submit.

Examples Gallery

Form submission with dirname, JavaScript dirName property, and dirname vs dir explained.

Example — Submit Direction with a Comment

A textarea that sends both the comment and its direction:

comment-form.html
<form method="get" action="/submit">

  <label for="comment">Comment</label>

  <textarea

    id="comment"

    name="comment"

    dir="auto"

    dirname="comment-dir">Hello</textarea>

  <button type="submit">Send</button>

</form>
Try It Yourself

How It Works

The browser sends the textarea value under comment and the detected direction under the name you chose in dirname.

Dynamic Values with JavaScript

Set the direction field name with the dirName DOM property (camelCase):

dirname-js.html
<input type="text" id="dynamicText" name="message" dir="auto">



<script>

  const input = document.getElementById("dynamicText");

  input.dirName = "message-dir";

</script>
Try It Yourself

How It Works

In JavaScript the property is dirName (camelCase), while the HTML attribute is dirname (lowercase).

Example — dirname vs dir

Do not confuse the two attributes:

wrong-vs-right.html
<!-- Wrong: dirname is not ltr/rtl -->

<input dirname="ltr">



<!-- Correct: dirname is a field name -->

<input dir="auto" dirname="message-dir">

How It Works

dir sets how text appears. dirname names the extra field the server reads to learn that direction on submit.

♿ Accessibility & UX

  • Use dir="auto" with dirname — Lets users type in their natural direction while the server still receives direction metadata.
  • Store direction server-side — Preserve ltr / rtl when re-displaying user comments in multilingual apps.
  • Do not replace langdirname reports direction, not language; still use lang where appropriate.
  • RTL layout still matters — Submitting direction does not mirror your page layout; use CSS logical properties for RTL UI.

🧠 How dirname Works

1

Author adds dirname

Set a field name like comment-dir on the control.

Markup
2

User types text

Browser detects LTR or RTL from dir and content.

Input
3

Form is submitted

Two pairs sent: value + direction field.

Submit
=

Server knows direction

Backend can store and render multilingual input correctly.

Browser Support

The dirname attribute is supported in modern browsers. It is a newer HTML feature; test form submission in your target browsers.

HTML5 · Modern browsers

Form direction submission

Chrome, Firefox, Safari, and Edge support dirname on textarea and supported input types.

95% Browser support
Google Chrome Supported
Full support
Mozilla Firefox Supported
Full support
Apple Safari Supported
Full support
Microsoft Edge Supported
Full support
dirname attribute 95% supported

Bottom line: Use dirname in modern apps; provide a hidden-field fallback for very old browsers if needed.

💡 Best Practices

✅ Do

  • Use a clear dirname field name (e.g. comment-dir)
  • Pair dirname with dir="auto" on the same control
  • Store submitted direction on the server
  • Use on textarea for multilingual comments
  • Test GET/POST query strings in devtools

❌ Don’t

  • Set dirname to ltr or rtl (that is dir's job)
  • Use dirname on unsupported input types
  • Confuse dirname with the dir attribute
  • Assume dirname changes visual direction alone
  • Skip server-side validation of submitted text

Conclusion

The dirname attribute is a practical tool for multilingual forms. It submits the text direction of user input as a separate form field so your server can process LTR and RTL content correctly.

Remember: the attribute value is a field name, not a direction keyword. Combine dirname with dir for display and submission together.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about dirname

Bookmark these before building multilingual forms.

5
Core concepts
💬 02

Form Submit

Extra direction pair.

Purpose
📝 03

textarea

Primary element.

Scope
↔️ 04

vs dir

Submit vs display.

Compare
⚙️ 05

dirName

JS property.

Dynamic

❓ Frequently Asked Questions

It names an extra form field that submits the text direction (ltr or rtl) of the control’s value when the form is submitted.
No. The attribute value is a field name like comment-dir. The browser sends ltr or rtl as the value of that field.
<textarea> and <input> types hidden, text, search, url, tel, and email.
dir controls display direction. dirname names the form field that submits direction to the server.
Use element.dirName (camelCase) on input and textarea elements.
Yes in current Chrome, Firefox, Safari, and Edge. Test form submission in your supported browser list.

Submit text direction in forms

Practice dirname with textarea and dir="auto" in the Try It editor.

Try form submit example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

3 people found this page helpful