Get meta value of td (React Js

Viewed 80

Lets say I already have this <td meta="Special forces" >View</td> and I need to get meta. How can I get it? I am using react js and using document.getelementByTagName() is bit tricky. Advice please

2 Answers

Since you're using React, you can make use of ref. Please find the below example which I've tried.

import { useEffect, useRef } from "react";

export default function App() {
  const ref = useRef();
  useEffect(() => {
    console.log(ref.current.getAttribute("meta"));
  });
  return (
    <div className="App">
      <table>
        <tbody>
          <tr>
            <td ref={ref} meta="Special forces">
              View
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  );
}

Try to look the code here in Sandbox here which I tried.

If you want to get every td element with meta attirbute, you can use querySelector with td[meta].

const elements = document.querySelectorAll('td[meta]');
console.log(elements);
<table><tr><td meta="Special forces" >View</td></tr></table>

btw, there is no meta attribute in td tag in HTML5.

Related