0

I get error cannot read property name of null when I try to log the name of target element.

 handle_text_input_change = (event) => {
    this.props.onChange(event);
    this.setState({text_input_value: event.target.value}, () => 
        {this.validate(event)});
 };

 validate = (event) => {
    let name = event.target.name;
    console.log("target name", name);
 }

1 Answer 1

3

The target identifier is not known in the validate function's scope. Did you mean name?

let name = event.target.name;
console.log("target name", name);
Sign up to request clarification or add additional context in comments.

3 Comments

@gayana Yes, if you pass in the event with parameter name event, you can access the name with event.target.name. You did this and saved the name in the local variable name, but in your console.logstatement you don't use the variable name or event.target.name, you used target.name which is not declared in that function.
hmm true...now within the validate method when i define as below, validate = (event) => { let name = event.target.name; console.log("target name", name); if (event.target.ValidityState.valueMissing) { this.setState({is_valid: false}); this.setState({error: {name} + 'cannot be empty'}); }}; i get cannot error can't read property valuemissing of null. Can u help me with this. thanks.
@gayana Thats another question regarding regarding ValidityState in combination with react, ask a new question instead providing code in comments. I can't help you with that one.

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.