Magento Extension – Change Text Box Value OnChange of Select in system.xml

magento-enterprisemodulesystem.xml

I have added select option SFTP host with values production, sandbox in my system.xml file of custom extenstion. There is one text field SFTP port in system.xml. I want to set the text field value on change of selection.

If I select production , then port value = 22
If I select sandbox , then port value = 21

<ftphost translate="label">
<label>SFTP host</label>
<frontend_type>select</frontend_type>
<source_model>gb/system_config_source_sftp</source_model>
<sort_order>60</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</ftphost>
<ftpport translate="label">
 <label>SFTP port</label>
 <frontend_type>text</frontend_type>
 <sort_order>70</sort_order>
 <show_in_default>1</show_in_default>
 <show_in_website>1</show_in_website>
 <show_in_store>1</show_in_store>
 </ftpport>

new system.xml

<liveenvironment translate="label">
                            <label>Live environment</label>
                            <frontend_type>select</frontend_type>
                            <source_model>adminhtml/system_config_source_yesno</source_model>
                             <comment><model>gb_gbgsp/system_config_source_sftpcomment</model>
     </comment>
                            <sort_order>1</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>1</show_in_website>
                            <show_in_store>1</show_in_store>
                        </liveenvironment>

comment.php

<?php 
class gb_gbgsp_Model_Adminhtml_System_Config_Source_Sftpcomment extends Mage_Core_Model_Config_Data
{
    public function getCommentText(Mage_Core_Model_Config_Element $element, $currentValue)
    {

    $result = "<script type='text/javascript'>    

            function init_comment()
            {

                $('#carriers_gbgsp_liveenvironment').observe('change', function(){
                    var field_value = $('#carriers_gbgsp_liveenvironment').getValue();
                    alert(field_value);
                });
            }
            document.observe('dom:loaded', function(){init_comment();});
            </script>";

        return $result;
    }
}

Best Answer

you can add your js through comment model

<ftphost translate="label">
    <label>SFTP host</label>
    <frontend_type>select</frontend_type>
    <source_model>gb/system_config_source_sftp</source_model>
    <comment> <![CDATA[<script type='text/javascript'>    

        function init_comment()
        {

            $('yourslelectid').observe('change', function(){
                var field_value = $('yourslelectid').getValue();
                //add your condition here
            });
        }
        document.observe('dom:loaded', function(){init_comment();});
        </script>]]
     </comment>
    <sort_order>60</sort_order>
    <show_in_default>1</show_in_default>
    <show_in_website>1</show_in_website>
    <show_in_store>1</show_in_store>
    </ftphost>