-1

My render function is

<input type="text" id="search" data-id={form.id} value={form.name} placeholder= "Search..." name="id" onKeyUp={this.keyUpFn}"/>

and my keyUpFn function is

keyUpFn(e){
    var textInput = document.getElementById('search');
    value1 = textInput.getAttribute('data-id')
    value2 = e.getAttribute('data-id')
    console.log(value1 )
    console.log(value2 )
}

Both console value gives error as getAttribute is not defined. How can I solve this in React?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hareesh S
  • 197
  • 1
  • 2
  • 12

3 Answers3

0

You can get the data from the Event target object. This is how the code would look

keyUpFn(e){
    console.log(e.target.getAttribute('data-id'));
    console.log(e.target.value);
}
GSSwain
  • 5,787
  • 2
  • 19
  • 24
0

You can actually get the data requested from the event (e) in your function by accessing

event.currentTarget.dataset.paramater;

keyUpFn(e) {
  e.currentTarget.dataset.id
}
Mirrorcell
  • 384
  • 2
  • 10
0

You can use getElementsByClassName (or id, name etc.), querySelectorAll, focus. First two returns an array of all valid elements.