Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript React Router Basics Going Further with Routes Navigating Routes Programmatically

Getting a error with e.target.elements on form submission

I’m trying to implement Guil’s onSubmit handler but I’m getting a Uncaught TypeError: Cannot read property '0' of undefinedin reference to e.target.elements. It appears that the elements array does not exist… Any help would be appreciated Guil Hernandez

2 Answers

Adriaan Janse van Rensburg
Adriaan Janse van Rensburg
2,708 Points

It is hard to figure out what is going on without seeing your code, but here is my handleSubmit function in the Home.js file.

handleSubmit(event){
    event.preventDefault();
    let teacherName = event.target.elements[0].value;
    let teacherTopic = event.target.elements[1].value;
    let path = `featured/${teacherTopic}/${teacherName}`;
    browserHistory.push(path);
  }

Hopefully the finished function can help you out :)

Thanks, that worked

It actually turned out that I misspelled elements as eements somehow!

The ` made the difference for mine as I was using ' at first!

Yeah I think that was also my issue; I didn’t realize that template strings needed a backtick instead of a normal quote :)