# Auto Populate

**URL:** <https://forums.knack.com/t/auto-populate/8503>\
**Category:** Get Answers\
**Created:** [March 10, 2020, 6:14pm UTC](https://forums.knack.com/t/auto-populate/8503 "2020-03-10T18:14:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MarkCrum79347](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@MarkCrum79347](https://forums.knack.com/u/MarkCrum79347)\
**Post date:** [March 10, 2020, 6:14pm UTC](https://forums.knack.com/t/auto-populate/8503/1 "2020-03-10T18:14:07Z")

</div>

How do I auto Populate a field. For example is I tag a customer I want it to populate the address field.

---

<div class="post-metadata">

**Author:** ![Shrey](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@Shrey](https://forums.knack.com/u/Shrey)\
**Post date:** [March 12, 2020, 3:55pm UTC](https://forums.knack.com/t/auto-populate/8503/2 "2020-03-12T15:55:29Z")

</div>

9212997488 Crum&nbsp;Hi Mark. If i understood you correctly, you should try this code:

&nbsp;

$(document).on('knack-view-render.view\_XX', function(event, view) {  
&nbsp; var customer = document.querySelector("select#view\_XX-field\_YY.select");&nbsp; &nbsp;// view\_XX is your Form view. field\_YY is your connection (Customer) field  
&nbsp; function get\_address(id, callback){  
&nbsp; let headers = {'X-Knack-Application-Id': 'Your Application ID', 'X-Knack-REST-API-Key': 'knack', 'Authorization': Knack.getUserToken()};  
&nbsp; let api\_url = '[https://api.knack.com/v1/pages/scene\_A/views/view\_B/records/](https://api.knack.com/v1/pages/scene_A/views/view_B/records/)' + id;&nbsp; // This should point to a table view in your app where at least the one of the columns is for field\_YY and the view should 'technically' be available to the logged-in user&nbsp;&nbsp;  
&nbsp; $.ajax({  
&nbsp; &nbsp; url: api\_url,  
&nbsp; &nbsp; type: 'GET',  
&nbsp; &nbsp; headers: headers,  
&nbsp; &nbsp; success: function(response) {  
&nbsp; &nbsp; &nbsp; callback(response);  
&nbsp; &nbsp; }  
&nbsp; })  
}  
function put\_address(data){  
&nbsp; if(data.field\_ZZ !== "" && data.field\_ZZ !== undefined){&nbsp; &nbsp;// field\_ZZ is your Address field from the Customer record  
&nbsp; document.querySelector("#kn-input-field\_WW #street").value = data.field\_ZZ\_raw.street;&nbsp; // field\_WW is the Address field in your Order form  
&nbsp; document.querySelector("#kn-input-field\_WW #street2").value = data.field\_ZZ\_raw.street2;  
&nbsp; document.querySelector("#kn-input-field\_WW #city").value = data.field\_ZZ\_raw.city;  
&nbsp; document.querySelector("#kn-input-field\_WW #state").value = data.field\_ZZ\_raw.state;  
&nbsp;&nbsp;document.querySelector("#kn-input-field\_WW #zip").value = data.field\_ZZ\_raw.zip;  
} else {  
&nbsp; alert("No address found! ");  
&nbsp; console.error("No address found! ");  
}  
}  
function clear\_address(){  
&nbsp; document.querySelector("#kn-input-field\_WW #street").value = "";  
&nbsp; document.querySelector("#kn-input-field\_WW #street2").value = "";  
&nbsp; document.querySelector("#kn-input-field\_WW #city").value = "";  
&nbsp; document.querySelector("#kn-input-field\_WW #state").value = "";  
&nbsp; document.querySelector("#kn-input-field\_WW #zip").value = "";  
}  
$("select#view\_XX-field\_YY.select").on('change', () =\> {  
&nbsp; if(customer.value === "" || customer.value === undefined){  
&nbsp; &nbsp; clear\_address();  
&nbsp; &nbsp; console.log("No Customer selected!");  
&nbsp; } else {  
&nbsp; &nbsp; get\_address(customer.value, put\_address);  
&nbsp; }  
});

});

---

<div class="post-metadata">

**Author:** ![MarkCrum79347](https://avatars.discourse-cdn.com/v4/letter/m/258eb7/32.png) [@MarkCrum79347](https://forums.knack.com/u/MarkCrum79347)\
**Post date:** [March 11, 2020, 3:18pm UTC](https://forums.knack.com/t/auto-populate/8503/3 "2020-03-11T15:18:14Z")

</div>

I am not sure why? Here is what I want to do! Photos are inserted&nbsp;

![](upload://jRcd0U9tNupYNwMxyBWWgEutnjt.png)![](upload://awBaStGZqZrNp6PBJpJYlBC9eZZ.png)

---

<div class="post-metadata">

**Author:** ![BradStevens](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/bradstevens/32/140_2.png) [@BradStevens](https://forums.knack.com/u/BradStevens)\
**Post date:** [March 11, 2020, 10:26am UTC](https://forums.knack.com/t/auto-populate/8503/4 "2020-03-11T10:26:55Z")

</div>

Hi Mark - default field values not enough?

I think you might have more complex requirements that needs further explanation?
