Komarzer
Komarzer

Reputation: 637

Change value of dropdown with javascript

I'm using codeigniter framework and I have some fields that I want to fill when I check a checkbox (it takes data from other fields).

enter image description here

The thing is I want to fill the dropdown list with the value of an other dropdown list.

Here's the javascript code that I use to fill the fields :

function FillinEmail(info) {

                if (info.checked)
                {
                    document.getElementById('adresse_fact').value = document.getElementById('adresse').value;
                    document.getElementById('npa_fact').value = document.getElementById('npa').value;
                    document.getElementById('nomprenom_fact').value = document.getElementById('nom').value + ' ' + document.getElementById('prenom').value;
                }
                else
                {
                    document.getElementById('adresse_fact').value = '';
                    document.getElementById('npa_fact').value = '';
                    document.getElementById('nomprenom_fact').value = ''; 
                }
              }

And here is how I do my dropdown list with codeigniter :

<div class="form-group">
              <label class="col-md-2 control-label" for="id_npa_localite">Localité</label>   
              <div class="col-md-4">
                 <?php echo form_dropdown('id_npa_localite', $id_localite, null,'class="form-control"')?>
              </div>
           </div>

The null value is where I can put a default value, and that's what I would change in the javascript method, but I don't know how to do that in that case, since the other elements are html elements, it was easy to do.

Upvotes: 0

Views: 52

Answers (1)

CodeGodie
CodeGodie

Reputation: 12132

I would give that select an ID, and use innerHTML in your JS. Do it in this manner:

<select id="the_other">
    <option>test1</option>
    <option>test2</option>
    <option>test3</option>
</select>

<input type="checkbox" onchange="FillinEmail(this)">

<div class="form-group">
    <label class="col-md-2 control-label" for="id_npa_localite">Localité</label>
    <div class="col-md-4">
        <?php echo form_dropdown('id_npa_localite', $id_localite, null, 'id="ci_form" class="form-control"') ?>
    </div>
</div>

<script>
    function FillinEmail(info) {
        document.getElementById('ci_form').innerHTML = document.getElementById('the_other').innerHTML;

        /*
         if (info.checked) {
         document.getElementById('adresse_fact').value = document.getElementById('adresse').value;
         document.getElementById('npa_fact').value = document.getElementById('npa').value;
         document.getElementById('nomprenom_fact').value = document.getElementById('nom').value + ' ' + document.getElementById('prenom').value;
         }
         else {
         document.getElementById('adresse_fact').value = '';
         document.getElementById('npa_fact').value = '';
         document.getElementById('nomprenom_fact').value = '';
         }
         */
    }
</script>

Upvotes: 1

Related Questions