2
<ul>
    <li id="1" onClick={this.currentvalue}>2</li>
    <li id="2" onClick={this.currentvalue}>3</li>
    <li id="3" onClick={this.currentvalue}>4</li>
    <li id="4" onClick={this.currentvalue}>5</li>
</ul>

In onClick event, I have given a currentValue handler

public currentvalue(e:React."WHICH EVENT I HAVE TO USE HERE"<WHICH HTML 
ELEMENT?>)
{
   global.console.log(e.target.value)
}

In the handler which event I have to use to get the target value?

  • getting `value` instead of `text` seems invalid to me. should be `event.target.innerText` – Manoz Jul 19 '18 at 09:44

5 Answers5

0

Its s React.ChangeEvent<HTMLInputElement>.

public currentvalue(e: React.ClickEvent<HTMLInputElement>)
{
   global.console.log(e.target.value)
}
RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
0

If you asking about type of event, you can use Event type:

public currentvalue(e: Event)
{
   global.console.log(e.target.value)
}

https://developer.mozilla.org/en-US/docs/Web/API/Event

Azamat Zorkanov
  • 779
  • 1
  • 4
  • 9
0

For accessing the value you need to include value attribute in the list elements as

<ul>
<li id="1" value="2"onClick={this.currentvalue}>2</li>
<li id="2" value ="3" onClick={this.currentvalue}>3</li>
<li id="3"  value ="4" onClick={this.currentvalue}>4</li>
<li id="4"  value ="5" onClick={this.currentvalue}>5</li>
</ul>

The currentvalue method can be as

public currentvalue(e:Event)
{
   global.console.log(e.target.value)
}
Harikrishnan
  • 1,097
  • 8
  • 13
0

An elegant way may to bind the value to the function

wich result in something like this

public currentvalue(value:Integer, e:Event)
{
    global.console.log(value)
}

and in your component

<ul>
    <li id="1" onClick={this.currentvalue.bind(null, 2)}>2</li>
    <li id="2" onClick={this.currentvalue.bind(null, 3)}>3</li>
    <li id="3" onClick={this.currentvalue.bind(null, 4)}>4</li>
    <li id="4" onClick={this.currentvalue.bind(null, 5)}>5</li>
</ul>

to get it clear hear an working example https://codesandbox.io/s/6jl292nww

42tg
  • 695
  • 6
  • 12
0

Try to use

public currentvalue(e:React.MouseEvent<HTMLLIElement>)
{
   global.console.log(e.target.value)
}