# Set Form Field Value on Render with Javascript

**URL:** https://forums.knack.com/t/set-form-field-value-on-render-with-javascript/17544
**Category:** Get Answers
**Created:** [July 10, 2024, 6:59pm UTC](https://forums.knack.com/t/set-form-field-value-on-render-with-javascript/17544 "2024-07-10T18:59:58Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![brendanosu80224](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@brendanosu80224](https://forums.knack.com/u/brendanosu80224)
#### Post date: [July 10, 2024, 6:59pm UTC](https://forums.knack.com/t/set-form-field-value-on-render-with-javascript/17544/1 "2024-07-10T18:59:58Z")

</div>

The existing sample code in the documentation doesn’t work (below) for setting the value of a blank field on the form render. I am wondering if anyone knows how this can be done now.

```auto
$(document).on('knack-view-render.view_52', function(event, view, data) {
	// The connected record id value
	var connectionId = "5b2ac7fc66b6036b8f859c45";

	// Update the view and field ids for your application
	$('#view_52-field_3').val(connectionId);
	$('#view_52-field_3').trigger("liszt:updated");
});

```

---

<div class="post-metadata">

### Author: ![Les](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/les/32/4243_2.png) [@Les](https://forums.knack.com/u/Les)
#### Post date: [July 10, 2024, 7:55pm UTC](https://forums.knack.com/t/set-form-field-value-on-render-with-javascript/17544/2 "2024-07-10T19:55:57Z")

</div>

Hi @brendanosu80224,

Thank you for sharing! Please consider reaching out to our support team to report this [sample code](https://docs.knack.com/docs/javascript-jquery-examples#set-connection-field) as not working for your app. They will be able to troubleshoot this for you and report the issue to our engineering team if it ends up being a platform issue.

You can reach out to them via this form: [Create Support Ticket](https://learn.knack.com/article/kb-tickets/new)

---

<div class="post-metadata">

### Author: ![brendanosu80224](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@brendanosu80224](https://forums.knack.com/u/brendanosu80224)
#### Post date: [July 10, 2024, 8:42pm UTC](https://forums.knack.com/t/set-form-field-value-on-render-with-javascript/17544/3 "2024-07-10T20:42:06Z")

</div>

Hi Les,

I already reached out and they confirmed the sample code isn’t working and referred it to engineering.

Thanks

---

<div class="post-metadata">

### Author: ![CSWinnall](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/cswinnall/32/3631_2.png) [@CSWinnall](https://forums.knack.com/u/CSWinnall)
#### Post date: [July 12, 2024, 6:43am UTC](https://forums.knack.com/t/set-form-field-value-on-render-with-javascript/17544/4 "2024-07-12T06:43:28Z")

</div>

Hi @brendanosu80224

Can you please post your code that you used.

We use this code quite a lot through our app.

Craig

---

<div class="post-metadata">

### Author: ![brendanosu80224](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@brendanosu80224](https://forums.knack.com/u/brendanosu80224)
#### Post date: [July 12, 2024, 6:29pm UTC](https://forums.knack.com/t/set-form-field-value-on-render-with-javascript/17544/5 "2024-07-12T18:29:30Z")

</div>

hey,

the code posted in the first message is the code i am using.

thanks  
brendan

---

<div class="post-metadata">

### Author: ![CSWinnall](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/cswinnall/32/3631_2.png) [@CSWinnall](https://forums.knack.com/u/CSWinnall)
#### Post date: [July 12, 2024, 10:14pm UTC](https://forums.knack.com/t/set-form-field-value-on-render-with-javascript/17544/6 "2024-07-12T22:14:41Z")

</div>

Hi @brendanosu80224

If that’s the case then the issue is that the code is an example snippet it’s not to be copied and used.

There are 3 variables

1. View Id= view\_xyz this can be found when you select the view in the builder then look at the URL and the view\_… Is your viewId
2. Field Id= field\_123 you can find this in the builder by going to data the select the object then just above the fields section is a toggle to turn field IDs on or off then find the field for the select.
3. Connection Id - this is a little trickier to get dynamically but you can usually find this in the app URL somewhere depending on what you are trying to do if the id you’re after is not there then you can use a view based API calls to get this.

If you want more help with this then please DM me and we can set up a meeting to discuss further.

This is not an easy thing to do it took us about a year to figure it out and now we use this quite a lot in our app.

Craig

---

<div class="post-metadata">

### Author: ![brendanosu80224](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@brendanosu80224](https://forums.knack.com/u/brendanosu80224)
#### Post date: [July 12, 2024, 10:55pm UTC](https://forums.knack.com/t/set-form-field-value-on-render-with-javascript/17544/7 "2024-07-12T22:55:59Z")

</div>

hey @CSWinnall,

i appreciate it. i understand how this code is supposed to work. i use javascript in my apps. the issue is that this method not working, which knack’s support has already confirmed for me and referred the issue to engineering because the sample code they use in the documentation no longer works. they also told me that they don’t support javascript issues, so referred me here in case people have had success using other methods.

thanks  
brendan

---

<div class="post-metadata">

### Author: ![CSWinnall](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/cswinnall/32/3631_2.png) [@CSWinnall](https://forums.knack.com/u/CSWinnall)
#### Post date: [July 12, 2024, 11:04pm UTC](https://forums.knack.com/t/set-form-field-value-on-render-with-javascript/17544/8 "2024-07-12T23:04:27Z")

</div>

We use this code all the time a lot in our app probably about 10 different forms in variations of this code. It does work so not sure why support would say that.

---

<div class="post-metadata">

### Author: ![CSWinnall](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/cswinnall/32/3631_2.png) [@CSWinnall](https://forums.knack.com/u/CSWinnall)
#### Post date: [July 12, 2024, 11:13pm UTC](https://forums.knack.com/t/set-form-field-value-on-render-with-javascript/17544/9 "2024-07-12T23:13:41Z")

</div>

I’d be happy to take a look at your code to see if I can help.

I’m away for the weekend but can post some examples here on Monday

---

<div class="post-metadata">

### Author: ![CSWinnall](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/cswinnall/32/3631_2.png) [@CSWinnall](https://forums.knack.com/u/CSWinnall)
#### Post date: [July 15, 2024, 11:36am UTC](https://forums.knack.com/t/set-form-field-value-on-render-with-javascript/17544/10 "2024-07-15T11:36:17Z")

</div>

Hi @brendanosu80224

Here are some examples of us using this code in our live app:

`$('#view_4860-field_6144').val(newCareMngrId).trigger('liszt:updated');`

```auto
function addConnectionIdToRecord (viewId, conxFieldId, connectionId = null) {
        const conxId = connectionId ? connectionId : getRecordID(); //Get Record Id from URL if no record Id passed in
        const viewElement = $(`#${viewId}`).length ? $(`#${viewId}`) : $(`#connection-form-view:has(input[value="${viewId}"])`);
        const assessConx = viewElement.find(`#kn-input-field_${conxFieldId}`);

        assessConx.addClass(CLASS_HIDDEN);
        assessConx.find('select').val(conxId).trigger('liszt:updated');
    }

```

Both theses examples use the code in the example snippet from the dev wiki and are working fine in our app. The function addConnectionIdToRecord is used multiple times throughout our app.

Craig

---

<div class="post-metadata">

### Author: ![brendanosu80224](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@brendanosu80224](https://forums.knack.com/u/brendanosu80224)
#### Post date: [July 15, 2024, 11:40pm UTC](https://forums.knack.com/t/set-form-field-value-on-render-with-javascript/17544/11 "2024-07-15T23:40:44Z")

</div>

hey @CSWinnall,

i appreciate it. i just tested the method on a different account and it’s working there, so it appears to be an issue with my actual account or app. i’m just gonna make a copy of the problem app and see if that solves the problem.

thanks  
brendan
