Matt Martin
Matt Martin

Reputation: 51

hubspot onFormReady populate form field

I have a hubspot form embedded on a page and i'm trying to pre-populate a field with jQuery. I want to use the text that is between an H2 tag with the class "djc_title" that is elsewhere in the page and with it populate an input field with the ID of "productname" in the embedded form. Here is my code. I'm sure it's all wrong. Can someone point me in the right direction?

hbspt.forms.create({ 
    portalId: '',
    formId: '',
    onFormReady: function($form) {
        $(“#productname”).val($("h2.djc_title").text());
    }
});

Upvotes: 3

Views: 3780

Answers (2)

Sagar Rana
Sagar Rana

Reputation: 568

Below code work for me. It may help someone.

 hbspt.forms.create({
    portalId: "*******",
    formId: "formid********",
        onFormReady: function($form){
        $form.find('input[name="firstname"]').val('Brian').change();
}
})

You need to use onFormReady callback which will provide $form params. We can use it to populate the value as $form has the id of all the input fields.

  $form.find('input[name="email"]').val('[email protected]').change();

Upvotes: 2

Kieran Walkin
Kieran Walkin

Reputation: 1

I'm not sure but it may be helpful to update the onFormReady function to correspond to the Hubspot fields as described on the API docs.

Examples:

$('input[value="checkbox_1"]').prop('checked', true).change();
$('input[name="firstname"]').val('Brian').change(); 

There are some examples on this page:

http://developers.hubspot.com/docs/methods/forms/advanced_form_options

Upvotes: 0

Related Questions