Array
Intermediate

Form field value

This custom JavaScript variable retrieves the value of a specific form field. This can be useful if you want to track user interactions with a form or gather insights about the information users are submitting.

Usecase

There are numerous use cases for tracking form field values. For example, you may want to track which options a user selects in a dropdown field on your form, or track how users interact with a multi-step form on your website. You could also use this information for personalization, for instance, by displaying a custom message or offer based on a user’s form inputs.

Instructions

This is a simple example of a GTM custom JavaScript variable that retrieves the value of an input field with a specific ID.

Please replace ‘input_field_id’ with the actual ID of the input field you want to track.

JavaScript

function() {
    var inputField = document.getElementById('input_field_id');
    if (inputField) {
        return inputField.value;
    } else {
        return null;
    }
}

This JavaScript function gets the HTML element with the specified ID using document.getElementById(), and then returns the value of the input field. If no element with the specified ID is found, the function returns null.

Please note that this variable will retrieve the value of the form field at the time the variable is run. Therefore, you should ensure that the variable is run after the form field value has been set. For instance, you could trigger the variable when the form is submitted.

Also, keep in mind that the ID of each element on a page should be unique. If there are multiple elements with the same ID, document.getElementById() will only return the first one.

Isn’t this the macro you were looking for? Check-out related macro’s below or request one.

Related macros

All macro’s

Capture UTM parameters and save them into separate cookies

Array
Intermediate

Capture UTM parameters

Array
Intermediate

Referrer information

Array
Intermediate