0

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.

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I didn't know I could put an ID on the form dropdown, I used value instead of innerHTML and it worked. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.