5

I have a dynamic html page, and I have an element (div.id=myComponent) in this html, that has three input element inside.

The direction of the body element is rtl and div.id="myComponent" element is ltr like this:

<html>
<body style="direction: rtl;">
    <input type="text" />  <!-- I can not set tabindex for this elements. beacuse these are generate the dynamically. -->
    ...
    <!-- any elements -->
    <div style="direction: ltr;" id="myComponent">
        <span> <input tabindex="3" type="text" placeholder="y" /></span>
        <span><input tabindex="2" type="text" placeholder="m" /></span>
        <span><input tabindex="1" type="text" placeholder="d" /></span>
    </div>
    <input type="text"  />
    ...
</body>

</html>

I need it, when the focus enters the div.id="myComponent", like below:

d => m => y

but is reverse.

I use the tabindex attribute, but it is not working correctly.

How do you fix this problem?

Thanks.

4
  • Is there any reason you cannot just reorder the elements in the HTML for myComponent rather than use the tabindex attribute? Use of the tabindex attribute is considered a bit of an anti-pattern. Commented Nov 7, 2019 at 5:57
  • the myComponent has been used in many place. in that case, I should reorder the elements in the all of documents. Commented Nov 7, 2019 at 6:06
  • with only HTML and CSS ? NO javascript ? Commented Nov 12, 2019 at 16:39
  • @A.R.F Please check my answer. I believe it is working correctly Commented Nov 13, 2019 at 5:20

6 Answers 6

1
+50

Remove direction from div.id="myComponent" setting multiple direction will cause problem or is confusing. Rearrange the #myComponent elements.

    <body style="direction: rtl;">
        <input type="text" />  <!-- I can not set tabindex for this elements. beacuse these are generate the dynamically. -->
        ...
        <!-- any elements -->
        <div id="myComponent" >
            <span><input type="text" placeholder="d" /></span>
            <span><input type="text" placeholder="m" /></span>
            <span><input type="text" placeholder="y" /></span>
        </div>
        <input type="text"  />
    </body>

Working example can be found at here

Update:

  1. To set ltr direction to elements inside #myComponent use following css property -

    #myComponent > *{ 
     direction: ltr 
    }  
    
  2. To preserve the element placing direction in 'ltr', wrap the span elements inside other div with property float:left. The updated example can be found at here

Sign up to request clarification or add additional context in comments.

4 Comments

I need to that direcrion,
please look at update. If it it serves the purpose.
you changed the place of elements (changed the y and d input). I can not change. it's used a many of my source places.
Of all the answers, I know your answer is close to my answer. So I vote for your answer. Thanks
1

You just simple to change your tabindex

    <html>
<body style="direction: rtl;">
    <input type="text" />  <!-- I can not set tabindex for this elements. beacuse these are generate the dynamically. -->
    ...
    <!-- any elements -->
    <div style="direction: ltr;" id="myComponent">
        <span> <input tabindex="1" type="text" placeholder="y" /></span>
        <span><input tabindex="2" type="text" placeholder="m" /></span>
        <span><input tabindex="3" type="text" placeholder="d" /></span>
    </div>
    <input type="text"  />
    ...
</body>

</html>

Please try to focus on your div first by JS script

document.getElementById("myComponent").focus();

And tabindex should work like you expect

2 Comments

dear friend, t need to use without the javascript and ... only html or css.
No, I just mean, javascript part just for testing, it allow you to focus on div myComponent before you press Tab button. It use to adapt your pre-condition "when the focus enters the div.id="myComponent","
1

Body tag have style direction: rtl and child #myComponent have too direction: rtl. That means reverse and again reverse - "ymd" (body) reverse to "dmy" and (#myComponent ) reverse to "ymd" which is first order. Need to chose which one have to used. Example with body style="direction: rtl; only :

<body style="direction: rtl;">
    <input type="text" />  <!-- I can not set tabindex for this elements. beacuse these are generate the dynamically. -->
      ...
    <!-- any elements -->
    <div id="myComponent">
        <span><input type="text" placeholder="y" /></span>
        <span><input type="text" placeholder="m" /></span>
       <span><input type="text" placeholder="d" /></span>
    </div>
    <input type="text"  />
      ...
</body>

Tab-indexes are not relevant to this problem - you can added if are needed.

7 Comments

Body tag have style direction: rtl and child #myComponent have direction: ltr, Please read the question more carefully.
I'm sorry if I wasted time, but that creates the needed order "d => m => y". You can see this. I'll see how this happens, because it confused me.
thanks for attension. I saw your link.But is moves from y=>m=>d.that should be d=>m=>y.
Sorry (again) i changed pen with #myComponent { display: flex; flex-direction: column-reverse; } . I think that is your solution.
Dear friend, I saw and test your code again, But's it's not working. do you test your code? It's moves from y=>m=>d.
|
1

A negative value (usually tabindex="-1") means that the element is not reachable via sequential keyboard navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets with JavaScript. You can observe the Tabindex Accessibility example below to clear confusion.

A negative value is useful when you have off-screen content that appears on a specific event. The user won't be able to focus any element with a negative tabindex using the keyboard, but a script can do so by calling the focus() method.

tabindex="0" means that the element should be focusable in sequential keyboard navigation, but its order is defined by the document's source order.

A positive value means the element should be focusable in sequential keyboard navigation, with its order defined by the value of the number. That is, tabindex="4" is focused before tabindex="5", but after tabindex="3". If multiple elements share the same positive tabindex value, their order relative to each other follows their position in the document source. The maximum value for tabindex is 32767. If not specified, it takes the default value 0.

For More detail https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex

<html>
<body style="direction: rtl;">

</p>

    <input type="text" />  <!-- I can not set tabindex for this elements. beacuse these are generate the dynamically. -->
    ...
    <!-- any elements -->
    <div style="direction: ltr;" tabindex="0" id="myComponent">
        <span><input tabindex="0" type="text" placeholder="y" /></span>
         <span><input tabindex="0" type="text" placeholder="m" /></span>
         <span><input tabindex="0" type="text" placeholder="d" /></span>
    </div>
 
    
    <input type="text"  />
    ...
</body>

</html>

3 Comments

Dear friend, the focus does not come from out side the element to inside myComponent. and that's exactly my problem. please read the previous answer and comments more carefully. thanks.
@A.R.F Please run the above code snippet only this time only and confirm. Sorry for the inconvenience
oh no, I've tried this solution before.It's move y=>m=>d. thanks anyway.
1

My friend think that's what you want If the problem is not resolved you can leave a comment

<!DOCTYPE html>
<html>
<body style="direction: rtl;">
    <input type="text" />  
...
<!-- any elements -->
<div style="direction: ltr;" id="myComponent">
    <span> <input tabindex="3" type="text" placeholder="y" /></span>
    <span><input tabindex="2" type="text" placeholder="m" /></span>
    <span><input tabindex="1" type="text" placeholder="d" autofocus/></span>
</div>
<input type="text"  />
...

A file link contains the code you can try on different browsers

Comments

1

Remove all tabindexes, and float your date inputs to right to reverse the display but keep the dom ordered.

<html>
<body style="direction: rtl;">
    <input type="text" placeholder="t1"/>  <!-- I can not set tabindex for this elements. beacuse these are generate the dynamically. -->
    ...
    <!-- any elements -->
    <div style="direction: ltr;" id="myComponent">
        <span style="float:right"><input type="text" placeholder="d" /></span>
        <span style="float:right"><input type="text" placeholder="m" /></span>
        <span style="float:right"><input type="text" placeholder="y" /></span>
    </div>
    <input type="text" placeholder="t2" />
    ...
</body>

</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.