I am creating a landing page within HubSpot which will have a form to capture Lead Data. I need to be able to pass a value into a field based on some of the answers on the form.
For example, there are 3 questions - where they chose Yes/No for each. I want to populate a hidden field with the word ‘Qualified’ if they say Yes to 2 of the 3 questions.
I would rather do this with JS on the form so it is calculated at the time of form submission in real time, rather than with a workflow on contact create as when the contact is created if the value of that field is a specific value then it will go into our automated dialer system.
Is it possible on the form or landing page to have this in JS that can do this conditional logic to populate the hidden field?
Hi @AlanGrace,
Yes, you can use JavaScript to apply conditional logic on a HubSpot embedded form or landing page and populate a hidden field before submission. Here’s how you could approach.
What you need:
- A hidden field on the form (e.g., lead_status)
- JS logic to check the values of your Yes/No questions
- Modify the hidden field value before the form is submitted
Here’s an example solution:
- Add a hidden field in your HubSpot form:
- Internal name: lead_status
- Use this JS on your landing page (or embed page):
-
<script>
hbspt.forms.create({
region: “na1”,
portalId: “YOUR_PORTAL_ID”,
formId: “YOUR_FORM_ID”,
onFormReady: function($form) {
$form.on(‘submit’, function() {
const yesAnswers = [
$form.find(‘input[name=“question_1”]:checked’).val(),
$form.find(‘input[name=“question_2”]:checked’).val(),
$form.find(‘input[name=“question_3”]:checked’).val()
].filter(answer => answer === “Yes”).length;
if (yesAnswers >= 2) {
$form.find(‘input[name=“lead_status”]’).val(‘Qualified’);
} else {
$form.find(‘input[name=“lead_status”]’).val(‘Unqualified’);
}
});
}
});
</script>
-
Replace
question_1, question_2
, etc., with the actual internal names of your Yes/No fields in HubSpot.
Please note - This works only with embedded forms or custom modules where you can add JS. Not supported in native drag-and-drop forms without a developer module.
Hope this helps!
If this answer your query, help the community by marking it as a solution.
Hi Kannan,
Thanks for the reply - where is the option to add the JS though, I do not see a code block or area within the form. Also you mentioned a developer module?