Two selects with the same script

Asked

Viewed 359 times

1

I have a select that uses a script to fill the city field, through the selected state. The problem is that I need to add another select and when I add, only a select works.

<script src="http://cidades-estados-js.googlecode.com/files/cidades-estados-v0.2.js"></script>

<select id="estado" name="estado"></select>

    <select id="cidade" name="cidade"></select>
    <br/>
    <select id="estado1" name="estado1"></select>

    <select id="cidade1" name="cidade1"></select>

<script type="text/javascript">
      window.onload = function() {
        new dgCidadesEstados(document.getElementById('estado'), document.getElementById('cidade'), true);
      }
    </script>

<script type="text/javascript">
      window.onload = function() {
        new dgCidadesEstados(document.getElementById('estado1'), document.getElementById('cidade1'), true);
      }
    </script>

  • @I have 2 selects where I select the state, and when selecting the state, I select the cities of that state. But when adding the second, only ONE works.

1 answer

1


No need to create two functions window.onload just use one, and inside use another new dgCidadesEstados...


Most likely just being read a onLoad in the creation of the form, so it did not carry the other select

window.onload = function() {
  new dgCidadesEstados(document.getElementById('estado'), document.getElementById('cidade'), true);
  new dgCidadesEstados(document.getElementById('estado1'), document.getElementById('cidade1'), true);
}
<script src="http://cidades-estados-js.googlecode.com/files/cidades-estados-v0.2.js"></script>
  <select id="estado" name="estado"></select>
  <select id="cidade" name="cidade"></select><br/><br/>
  <select id="estado1" name="estado1"></select>
  <select id="cidade1" name="cidade1"></select>

  • Thank you, that’s exactly what it was.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.