Javascript: Change other fields based on selection

JtheBAB

New member
Dear all

I have a simple form with the fields:

phone_type_idphone_type (Select)
country_idcount (Select)
phone_area_co (text)
phonenumber (text)

When the user select a value in phone_type_idphone_type (select) the javascript should change the values of the other three fields.

So i tried with the help of ChatGPT4 to get it running. That was the output based on the facts that we gathered... but non of the scripts suggestions worked

JavaScript:
function dadabik_phone(field) {
  if (field.value == '2') {
    document.getElementById('country_idcountry').getElementsByTagName('input')[0].value = '1';
    document.getElementById('phone_area_code').getElementsByTagName('input')[0].value = '22';
    document.getElementById('phonenumber').getElementsByTagName('input')[0].value = '123';
  }
}

How can i change the other fields on this form based on the value that is selected in phone_type_idphone_type?

Thanks

-----------------------------

Your current DaDaBIK version​


You are using DaDaBIK version 11.9-Elba platinum, installed on 18.04.2023 (installation code: 1823863317aa3d0534), the latest version of DaDaBIK is 11.9-Elba released on 23.03.2023

You are running the latest release of DaDaBIK

In case you want to upgrade to a more powerful edition (from Pro to Enterprise/Platinum, from Enterprise to Platinum) please contact us.


System info​


PHP Version: 7.4.33


mysql version: 5.7.42


Web server: Apache


Client: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0


----------------------------------------
 

eugenio

Administrator
Staff member
Hello,
instead of adding custom code, have you tried to use cascade fields? They may work in your case. Check the inline help in form configrator for the cascade parent field parameter.

Best,
 

JtheBAB

New member
Hi

Cascade fields are interesting but unfortunately not in this case.

phone_type_idphone_type defines if the phone is internal or external. And when it is internal it should already prefill country_idcount and phone_area_code. Additional it should add in the field phonenumber some required numbers. So that the users can just type the last 4 digits of the number.

Thanks
 
Top