Extract element value in react

Viewed 730

calling function onclick event

<th onClick={this.sort}>Grades</th>

and in Function, trying to get the value of text.

  sort(e){
    console.log(e.target);

  }

e.target catch <th>Grades</th>

how can just get text ie Grades' without th

1 Answers

You can get the text inside the element via innerText:

sort (e) {
    console.log(e.target.innerText);
}

However, in this case it may be better to pass the text in directly, depending on your use case:

<th onClick={() => this.sort('Grades')}>Grades</th>
Related