Skip to content
Advertisement

Trouble extracting Contact Form input to javascript function

I’ve been at this for hours, searching for answers, reading other people’s issues and I haven’t been able to fix this.

The setup: I’m hosting a website on AWS (S3 Static page), and I’d like a user to submit their input via a Contact Form. Inline javascript will intercept that input and send it to an SNS topic, which then emails me the information.

The issue: I get “null”, [object HTMLInputElement], or just blank values when I try to retrieve Name, Email, and Message inputs.

What I’ve tried:

  • document.querySelector(‘#Name’);
  • document.getElementById(‘Email’).value;
  • document.getElementById(‘Email’);

Any help would be greatly appreciated! Cognito and SNS are working perfectly, I just need to resolve this Javascript issue.

My code (I’ve trimmed it down to just the contact form):

JavaScript

Advertisement

Answer

The solution lies in how you are selecting your inputs and reading their values.

In the current state you store the value property in a variable, like with email and message. Doing this will store the value inside of the value property at that time. Which, when loading the page, is an empty string.

You can easily solve this by moving the reading of the value property inside of your LPAWS.sendToTopic function, which is triggered on the submit event.

By doing this you read the value property at the time that you submit the form.

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement