faisaljanjua
faisaljanjua

Reputation: 936

Extract element value in react

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

Upvotes: 3

Views: 886

Answers (1)

TimoStaudinger
TimoStaudinger

Reputation: 42520

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>

Upvotes: 4

Related Questions