Getting the current Job Post ID to the Wix form

Hi.

I added the Wix default Job Items, Item tempalte.
I added a Wix Form on the item.

There is a field “job_id” on the form.
I want to get the job id when a user submission the job apply form.

I have tried bwlow,

$w.onReady(function () {
    // Get the current dynamic page's item from the dataset
    const jobId = $w('#Jobs').getCurrentItem()._id; // Replace 'dataset1' with your actual dataset ID

    // If you have a form field with the ID 'jobIdField' (hidden field for job ID)
    $w('#job_id').value = jobId; // Set the job ID in the hidden field

    // Optionally, log the job ID for debugging purposes
    console.log("Job ID set to form:", jobId);
});

it says .getCurrentItem is not a function

So I try

$w.onReady(function () {
    // Wait for the dataset to load before accessing the current item
    $w('#Jobs').onReady(() => {
        // Now that the dataset is ready, retrieve the current item
        const currentItem = $w('#Jobs').getCurrentItem();

        // Check if the current item exists, then set the job ID
        if (currentItem && currentItem._id) {
            const jobId = currentItem._id;
            // Set the job ID in the hidden form field
            $w('#job_id').value = jobId;

            // Optionally log the job ID for debugging
            console.log("Job ID set to form:", jobId);
        } else {
            console.log("No job item found.");
        }
    });
});

it says TypeError: $w(…).onReady is not a function

I am very tired… Just want to geta ID. But spend hours have no solution…

Please help
Thanks

$w.onReady(() => {
    // Ensure the dataset is properly connected to the page
    const dynamicDataset = $w('#Jobs'); // Replace '#Jobs' with the correct ID of your dataset

    // Wait for the dataset to load
    dynamicDataset.onReady(() => {
        // Get the current item from the dataset
        const currentItem = dynamicDataset.getCurrentItem();

        if (currentItem && currentItem._id) {
            const jobId = currentItem._id; // Retrieve the job ID from the current item
            $w('#job_id').value = jobId; // Replace '#job_id' with your form field ID
            console.log("Job ID set to form:", jobId); // Debugging: Check if the ID is set
        } else {
            console.error("No job item found.");
        }
    });
});

The dynamic page data is accessed using the dynamic dataset connected to the page.
Ensure that the dataset is properly set up and connected to the relevant collection.
Use the getCurrentItem() method on the dataset, but only after it is ready.

Hello

It does not seem to work
instrument.js:109 TypeError: e.onReady is not a function

Am I putting the code in the wrong location?

I see the problem. You’re trying to get current item’s id auto generated when submitted. But what I see is you’re using the form element and not the Wix input elements. You need to make this using Wix input elements or replace the jobs element with the current dataset element available in your form page (you have to add the dataset from CMS tab)
And I guess you can remove the job_id element since I think it’s random id generation field and add some math.floor or math.random method.

1 Like

Hello

I can populate other value to the Wix Forms. I dont understand why the job ID cannot be. :face_with_spiral_eyes:

This is my code
I use this way to send member ID to the webhook. And I need to pass the job ID to the webhook as well. This is really important to create relationship on my airtable record

I got it resolved.

Turns out I have to use dynamicdatasetID instead of the collection ID…
… Wix is so differcult…

For first it it.
Next time, you’ll be passing this problem at once. Congratulations :tada: