0

I suppose to have some value from <button> tag but it is undefined when I click the button. What am I doing wrong?

I use function(e) to get attributes and then e.value = undefined.

runTest(elem){
    let url = 'someUrl';
    let path = elem.value;
    console.log(path);
    $.post(url, {
        path: path
    }, function(data, status){
        alert('Data: ' + data +  "\nStatus: " + status);
    });
}

render() {
    return (
        <div className='wrapper'>
            <div className="btn-group dropright">
                <button type="button" className="btn" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                    Wraith
                </button>
                <div className="dropdown-menu">
                    <button className="dropdown-item" value='someValue' onClick={this.runTest}>Get value</button>
                </div>
            </div>
        </div>
    );
}
4
  • onClick listener receives event object as argument, not DOM element. Commented May 6, 2019 at 15:29
  • you bind.this() the method? Commented May 6, 2019 at 15:29
  • 1
    this is not the way react apps usually work. better set that value to the props of your component and do something like <button className="dropdown-item" value={this.props.someValue} onClick={this.runTest}>Get value</button> Commented May 6, 2019 at 15:30
  • elem.traget.value was the answer Commented May 7, 2019 at 12:53

1 Answer 1

2

The onClick event fires an event.

So it's the event which is passed to your runTest function, not the element.

runTest(event) {
    console.log(event.target.value)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.