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

157
Views
React - accessing DOM value of parent element

I have a React component that renders a grid like this :

    for (let i=0; i<items.length; i++) {
        if (filterItem(items[i])) {
          rows.push(
            <div className="row" key={items[i]._id} id={items[i]._id} onClick={props.onClick}>
              {props.db !== "videos" && <div className="centered">{items[i].level}</div>}
              {props.db === "questions" && <div className="centered left">{items[i].question}</div>}
              {props.db !== "questions" &&  <div className="centered left">{items[i].instructions}</div>}
              <div className="centered" onClick={props.delete}><FontAwesomeIcon icon={ faTrashAlt }/></div>
            </div>
          );
        }
    }

I display some items from different databases and depending on the database the fields are not the same. I need to implement onClick handler which will either preview a video (in case of videos database is displayed) or get the id value of the grid row (in case of any other database is displayed)

My current code looks like this:

    handleClick(e) {
        
        if (this.state.database === "videos") {
          this.setState(state => ({
            videoPreview: this.getDbItem(e.target.innerText).link
          }))
        }

        else if (e.target.parentElement.id) {
          this.editMode(e.target.parentElement.id) //get id of the row div
    
        }
    
    }

and it works, but seems wrong...

Is there a better way to do this?

about 4 years ago · Juan Pablo Isaza
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!