skip to Main Content

I am trying to access the name of the variable in my html with this

  const changeCurrentType = (e) => {
    setCurrentType(e.target);
    console.log(e.target)
  }

The function is able to print this with e.target
enter image description here

How however, when I write

console.log(e.target.name) 

It prints undefined. May I know how can I access the name variable of div? In my case, how can I get the "subclub" from name="subclub"?

Thank you.

2

Answers


  1. Chosen as BEST ANSWER

    I managed to solve the question with this line of code to access the div

      const changeCurrentType = (e) => {
        setCurrentType(e.target.getAttribute('name'));
      }
      
    

  2. <div data-name="name"></div>
    
    const changeCurrentType = (e) => {
    setCurrentType(e.target.dataset); // {name: "name"} }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search