Converts Title/Label to an Input Field

Posted by: Nico Cagalitan Date Posted: October 29, 2026
Back to Case Studies

Issue

The client wants to track the title of the booth services they offer. This is to let them know which booths are getting traffic and leads.

Solution

Created a custom code that converts an HTML element (h3) to an input field, and gets tracked in LMFX.

<script>

(function () {

    var boothTitle = '';

    // remember which booth card was clicked (bubble phase, after the site's own handler)

    document.addEventListener('click', function (e) {

        try {

            var btn = e.target.closest && e.target.closest('.gallery-card__btn');

            if (!btn) { return; }

            var card = btn.closest('.gallery-card__text');

            var h3 = card && card.querySelector('h3');

            boothTitle = h3 ? h3.textContent.replace(/\s+/g, ' ').trim() : '';

        } catch (err) {}

    });

 

    function addBoothTitle(form) {

        var old = form.querySelector('input[name="booth_title"]');

        if (old) { old.parentNode.removeChild(old); }

        var input = document.createElement('input');

        input.type = 'hidden';

        input.name = 'booth_title';

        input.value = boothTitle;

        form.appendChild(input);

    }

 

    // the submit click still needs the capture phase, so the field exists before mcfx reads the form

    document.addEventListener('click', function (e) {

        try {

            var btn = e.target.closest && e.target.closest('#gform_submit_button_8');

            var form = btn && btn.closest('#gform_8');

            if (form) { addBoothTitle(form); }

        } catch (err) {}

    }, true);

    document.addEventListener('submit', function (e) {

        try {

            if (e.target && e.target.id === 'gform_8') { addBoothTitle(e.target); }

        } catch (err) {}

    }, true);

})();

</script>

Achievement

Able to create a custom code that uses other elements, e.g., (h3, p) to an input field

Snippet/Screenshot/Link

Ticket: https://app.webfx.com/todos/270384883