Submit custom code variables with form

Hi fellow developers,

I am looking for a solution to submit the calculations I have done in custom code via a form.

As an output from the custom code, I am calculating the variable “nights” which I then also transfer back as a webflow ID:

document.getElementById('nights').innerHTML = nights;

Is there any possible way I can either reference a webflow ID or a custom code variable in an HTML embed or use it as a prefill for a hidden input field?

I tried the following via an HTML element, but unfortunately it did not work…
image

Read-only link: Webflow - FurryPaws

Thank you very much for your help!!

Ah, you want to bind that to your form.

That’ll get you what you need :smile:

1 Like

Hey Jerome, you’re close.

I think you want this hidden field in your form;

<input type="hidden" name="number-nights" id="nights" value="">

And once you have your calculated value, you apply that with;

var nights = 3; // calculated value
document.getElementById("nights").setAttribute("value", nights);

Notes;

  • For getElementByID to work that ID has to be on the element you’re targeting, thus id="nights" on the input.
  • Creating that input makes life easy, but you have no value initially, thus I set it to blank.
  • innerHTML is for changing the inner contents of an element like <p></p>. An <input> element does not have content to change. Instead you’re wanting to change the value attribute, thus setAttribute
1 Like

Thank you @ChrisDrit for the links!

@memetican thank you so much, absolute legend, saved me a lot of time! Worked like a charm!

1 Like