# Javascript OnSelect change field label

**URL:** <https://forums.knack.com/t/javascript-onselect-change-field-label/14452>\
**Category:** API & Custom Code\
**Created:** [June 14, 2023, 10:07am UTC](https://forums.knack.com/t/javascript-onselect-change-field-label/14452 "2023-06-14T10:07:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mike7](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mike7](https://forums.knack.com/u/Mike7)\
**Post date:** [June 14, 2023, 10:07am UTC](https://forums.knack.com/t/javascript-onselect-change-field-label/14452/1 "2023-06-14T10:07:29Z")

</div>

Hi I know I can dynamically show/hide fields of a form. Is there a way I can dynamically change the label of one field based on the selection of an item in another multi-choice field using Javascript? I have had a look at the examples on [JavaScript & jQuery Examples](https://docs.knack.com/docs/javascript-jquery-examples) but can’t quite grasp which bits to combine? Cheers! Mike

---

<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:** [June 18, 2023, 8:02pm UTC](https://forums.knack.com/t/javascript-onselect-change-field-label/14452/2 "2023-06-18T20:02:01Z")

</div>

Hi @Mike7

You could do this by adding a change function to your multiple choice field which will get the selected result.

```auto
$('#view_xxxx-field_yyyy').change(function () {
       $('#kn-input-field_zzzz label').text($(this).val());
});

```

This will change the label to match the option selected in the dropdown.

If you would like to alter the text based on the multiple choice selected value you could add an if inside the change function.

```auto
// #view_xxxx is the view that the multiple choice is on and field_yyyy is the multiple choice field
$('#view_xxxx-field_yyyy').change(function () {
       if ($(`#view_xxxx-field_yyyy option:selected`).text() == 'Hello') {
           // zzzz is the field with label to change
           $('#kn-input-field_zzzz label').text('New Label Text Here');
       }
});

```

If you have lots of Ifs then you could use a case statement.

If you’d like any further help please let me know.

Craig

---

<div class="post-metadata">

**Author:** ![Mike7](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mike7](https://forums.knack.com/u/Mike7)\
**Post date:** [June 19, 2023, 7:58am UTC](https://forums.knack.com/t/javascript-onselect-change-field-label/14452/3 "2023-06-19T07:58:32Z")

</div>

Wow many thanks Craig. I have added the code and updated the ids but it doesn’t seem to be running. Is there anything else I need to add to make it is triggered?

// #view\_xxxx is the view that the multiple choice is on and field\_yyyy is the multiple choice field  
$(‘#view\_217-field\_289’).change(function () {

if ($(‘#view\_217-field\_289 option:selected’).text() == ‘Renewal’) {  
// zzzz is the field with label to change  
$(‘#kn-input-field\_105 label’).text(‘Opportunity Id’);  
}  
else {  
$(‘#kn-input-field\_105 label’).text(‘Deal Reg Number’);  
}  
});

---

<div class="post-metadata">

**Author:** ![Mike7](https://avatars.discourse-cdn.com/v4/letter/m/6f9a4e/32.png) [@Mike7](https://forums.knack.com/u/Mike7)\
**Post date:** [June 19, 2023, 8:01am UTC](https://forums.knack.com/t/javascript-onselect-change-field-label/14452/4 "2023-06-19T08:01:18Z")

</div>

Doh! I have answered my own question I was missing the

$(document).on(‘knack-view-render.view\_217’, function(event, view, data) {

etc lol

It’s all working now - thank you!

---

<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:** [June 19, 2023, 8:11am UTC](https://forums.knack.com/t/javascript-onselect-change-field-label/14452/5 "2023-06-19T08:11:11Z")

</div>

Brilliant I’m really glad you have got it working. I’ll make sure I add the trigger next time I answer a question.
