Populate field with dropdown

Hi all, as per the subject, what I would like to do is to populate the name field of a custom form with the same value selected from a dropdown menu created with Studio:

Name:
DropDown: box_1, box_2, box3...

so, by choosing box_2 the name field will have the value box_2

I have on other occasions populated fields, but never with dropdown, so far I had done it this way:

In editviewdfs, in the field to be populated:

...
'customCode' => '<input type="text" value = "{$fields.n_box_c.value}" id = "name" name="name" readonly="readonly">',
...

the same thing is needed in the quickcreatedefs file.

can anyone tell me how to proceed? Thank you.

Hi @Fab, i’m sorry, are you trying to populate the dropdown with values coming from a custom function? From the database?

No, the dropdown is static, I want to populate the “name” field of my form according to the dropdown choice

If you want that you could use a JavaScript function.
You can include a JavaScript file in custom/modules/<MODULE’S_NAME>/metadata/editviewdefs.php

such as:

$viewdefs ['Accounts'] = 
array (
  'EditView' => 
  array (
    'templateMeta' => 
    array (
      'form' => 
      array (
        'buttons' => 
        array (
          0 => 'SAVE',
          1 => 'CANCEL',
        ),
      ),
      'maxColumns' => '2',
      'widths' => 
      array (
        0 => 
        array (
          'label' => '10',
          'field' => '30',
        ),
        1 => 
        array (
          'label' => '10',
          'field' => '30',
        ),
      ),
      'includes' => 
      array (
        0 => 
        array (
          'file' => 'modules/Accounts/Account.js',
        ),
      ),

....

In truth I was thinking of not using it, which is why I had posted the example in the editviewdefs, but now I am trying it with js as well:
I created a simple js, to see if it worked–the js works.
Now I’m trying to populate the name field, but I don’t know why, I’m having difficulty:


...
$( "#n_box_c" ).change(function() {		
		var selection;
		
		selection=$("#n_box_c option:selected").html();
		
		$('#name').val(selection);
		alert(selection);
				
	});

...

It should be simple, but the “name” field is not being populated, “selection” I can see.

I think the problem is the “name” field, if instead of

$('#name').val(selection);

I use another custom field

$('#<custom_field>').val(selection);

the field is populated. Perhaps since the “name” field is an automatically created module’s own field, it has a special identification criterion or I cannot access its value to change it

ok, wrong identifier, I replaced

$('#name').val(selection);

with

$('input#name').val(selection);

now it works

1 Like