Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
How to add attribute to parent div with ClassName in ReactJS?

I'm using ReactJS material UI tabs navigation. I want to add one attribute named tabindex in one div with class=MuiTabs-flexContainer MuiTabs-centered having value -1 to its child nodes. I had tried but tabindex=-1 is getting added to somewhere else:

Here in this screenshot, I need tabindex=-1 in its child nodes i.e., button elements:

enter image description here

Here is the UI tabs part:

<Tabs value={value} id="tabSec">
  <Tab
    label={
      <span className={value === 0 ? "active-tab" : "custom-style-on-tab"}>
        ABC
      </span>
    }
  />

  <Tab
    label={
      <span className={value === 1 ? "active-tab" : "custom-style-on-tab"}>
        XYZ
      </span>
    }
  />
</Tabs>;

The function which I have done so far is:

const tabRoving = (element) => {
    element.addEventListener("keydown", function (e) {
      if (e.keyCode === 13) {
        if (element.contains(document.activeElement)) {
          element.childNodes.forEach(function (childElement) {
            if (childElement != document.activeElement) {
              childElement.setAttribute("tabindex", "-1");
              console.log("done");
            } else childElement.setAttribute("tabindex", "0");
          });
        }
      }
    });
  };

  useEffect(() => {
    tabRoving(document.getElementById("tabSec"));
  }, []);

How can I achieve this? Is this possible in ReactJS with material UI?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Have you tried passing a tabIndex={-1} prop on the Tab component? It should spread unused props down, eventually to the button.

Inheritance

While not explicitly documented above, the props of the ButtonBase component are also available on Tab. You can take advantage of this to target nested components.

Spread

Props supplied to a component which are not explicitly documented are spread to the root element; for instance, the className prop is applied to the root.

Pass a tabIndex prop to the Tab components:

<Tabs value={value} id="tabSec">
  <Tab
    tabIndex={-1}
    label={
      <span className={value === 0 ? "active-tab" : "custom-style-on-tab"}>
        ABC
      </span>
    }
  />

  <Tab
    tabIndex={-1}
    label={
      <span className={value === 1 ? "active-tab" : "custom-style-on-tab"}>
        XYZ
      </span>
    }
  />
</Tabs>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!