How do I autofill a textbox in a form with the current dynamic page url?

Hey community!

I am relatively new to the velo coding and want to implement a seemingly super easy piece of code.
I am creating a website for a vacation rental website and I’ve got dynamic pages set up with filters that all work perfectly. Now the thing is that I am building the page of the Item Details page where you can see the description, pictures, etcetera… I have added a form for visitors to fill in when they would like more information, would like to book their stay.
On this form I have a textbox called “link” and this should be filled automatically when sending the form with the current dynamic page’s url. So we get the exact url the visitor was interested in.

I am building in Wix Studio and I tried this code but I can’t seem to find the correct ID for the field on the form…

import wixData from ‘wix-data’;
import wixLocation from ‘wix-location’;

$w.onReady(function () {
let url = wixLocation.url;
$w(" #link ").value = url.toLocaleString()

Hi Bart,

Do you have some other code on the page as well, apart from this? If not, then you don’t need wix-data.

You can find the ID by clicking on the text input box and the properties panel will show up on the bottom right.

In the above example, #smartSpeakerImage is the ID of the image element.

And I’d recommend that you use the new wixLocationFrontend API instead.

import wixLocationFrontend from 'wix-location-frontend';

$w.onReady(function () {
    let url = wixLocationFrontend.url;
    $w("#myElementId").value = url;

P.S. I’m not sure if this will work with the new Wix Forms.

Hey Pratham
Thank you for taking the time to answer but this is not working. As you’ve mentioned in the PS it is indeed the new wix forms and I can’t see the ID for the individual fields of the form as in the previous builder.

Wix Forms is not codable.

You will have to build your firm using input elements AND you must set field value on the dataset (not set value to input element) in order to actually capture that data.

1 Like

Seemingly super easy as I thought… Never thought it would be that simple…
Thanks for the help.
This code worked perfectly and used this guide for the custom form building

import wixLocation from ‘wix-location’;

$w.onReady(function () {
let url = wixLocation.url;
$w(“#link”).value = url.toLocaleString()