Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

`HTMLElement.draggable` and `tabIndex`: dragging and focus

`draggable` controls whether an element can be a drag source. `tabIndex` influences whether and when Tab navigation reaches it. Setting one property does not replace setting the other.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

element.draggable
 element.draggable = true;  // or false
 element.tabIndex
 element.tabIndex = 0;     // or another integer

HTMLElement.draggable/tabIndex refers to two separate properties on HTML elements. draggable reads and sets a boolean that controls whether the element can be a native drag source. Assigning true or false writes the corresponding text "true" or "false" to the draggable attribute. The attribute is enumerated, not boolean: its presence alone does not enable dragging. A missing or invalid value uses the automatic default: images and links with href, for example, are draggable, but a plain div is not. Remove the attribute, rather than setting false, to restore that default.

tabIndex reads an integer and writes the tabindex attribute. 0 normally includes the element in the natural Tab order. A negative value, usually -1, normally skips it in that order but still allows focus() if the element can receive focus. Positive values go before 0 and default positions within the same focus navigation scope: lower values come first, with ties broken by tree order. Avoid positive values; use a logical document order instead. With no attribute, a button normally reports 0 and a plain div reports -1; a reading alone does not prove that focus will succeed, for example on a disabled control or in an inert region.

These are browser HTML APIs, not part of ECMAScript 2024. Setting tabIndex does not implement keyboard dragging, and setting draggable does not define what happens on drop. Behavior depends on the browser, platform, and input method. For reordering, also offer controls such as “Move up” and “Move down” buttons that work with keyboard and touch.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>draggable and tabIndex</title>
</head>
<body>
  <script>
    const element = document.createElement('div');
    const report = () => console.log(JSON.stringify([
      element.draggable, element.tabIndex,
      element.getAttribute('draggable'), element.getAttribute('tabindex')
    ]));

    report();
    element.draggable = true;
    element.tabIndex = 0;
    report();
    element.tabIndex = -1;
    report();
  </script>
</body>
</html>

Result

[false,-1,null,null]
[true,0,"true","0"]
[true,-1,"true","-1"]

Save the example as an `.html` file, open it in a browser, and check the developer console. Each line shows the two properties followed by their attributes. `null` means the attribute is absent. Changing `tabIndex` to `-1` leaves `draggable` unchanged; this example does not test gestures or focus movement.

Related lessons

  • JavaScript · Lesson 34 · Intermediate · Version: 3

Sources