avi.algaly 0 Newbie Poster

Setting a value to hidden element in clickdimensions is tricky beacuse you cant set an id to it and there are lots of hidden elements set by the page.
the way i did it was by setting a special value to that field and find it later when code runs.

Dani AI

Generated

Good tip from — marking the target hidden field with a unique sentinel value is a simple, reliable start. Below are a few more practical, low-risk patterns you can use when ClickDimensions produces many hidden inputs and you can’t rely on stable IDs.

Use a sentinel and replace it when the DOM is ready

// put a unique default value in the form builder (e.g. "__MY_SENTINEL__")
// then find and replace it in the page
var hid = Array.from(document.querySelectorAll('input[type="hidden"]'))
               .find(i => i.value === '__MY_SENTINEL__');
if (hid) hid.value = 'realValue';

Wait safely for the field (polling or MutationObserver)

// short polling
var tries = 0;
var iv = setInterval(function(){
  var el = document.querySelector('input[type="hidden"][name="target_field_name"]');
  if (el || ++tries > 30) {
    if (el) el.value = 'realValue';
    clearInterval(iv);
  }
}, 200);

// or use MutationObserver to react when elements are inserted

Set the value at submit time (prevents framework overwrite)

form.addEventListener('submit', function(){
  var hidden = form.querySelector('input[type="hidden"][name="target_field_name"]');
  if (hidden) hidden.value = document.getElementById('myVisibleSource').value;
});

Troubleshooting & cautions

  • Inspect the DOM to prefer a stable selector (name, data-attribute, or a unique parent container) over index-based picks.
  • Hidden inputs can be replaced or re-rendered; set values as late as possible (on submit or immediately after insertion).
  • Disabled inputs are not submitted — ensure the element is not disabled.
  • Never rely on client-side hidden fields for security or authoritative data; always validate on the server/CRM side.

If ClickDimensions lets you set a default value or map query-string values in the form builder, prefer that over DOM hacks. The sentinel + submit-time copy pattern is a pragmatic fallback when you must work purely in client JavaScript.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.