onchange Select does not work - jQuery

Asked

Viewed 4,496 times

0

Hello guys I’m doing the onchange on the tag select but not working, I’m using the plugin JS Google for cities and states, example:

JSFIDDLE: http://jsfiddle.net/fn7c57ag/1/

  • You want to execute the function of popular the city but also perform its function?

  • Here is working Chrome47

3 answers

2


your script cidadesEstados.js is setting the event .onchange() in the following passage:

this.estado.onchange=function(){this.dgCidadesEstados.run()};

with this what you defined in HTML is overwritten, so I advise you to use an eventListener:

var model = {};
document.addEventListener("readystatechange", function () {
  if (document.readyState == "interactive") {
    model = {
      estado: document.getElementById('estado'),
      cidade: document.getElementById('cidade'),
      change: true
    };    
    new dgCidadesEstados(model);
    
    //preste atenção no addEventListener;
    model.estado.addEventListener("change", myfunction);
  }
});

var myfunction = function (event) {
  alert(event);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://ffsolucoesweb.com/ferramentas/cidadesEstados.js"></script>
<label>Estado</label>
<input id="estado" name="estado">
<label>Cidade</label>
<input id="cidade" name="cidade">

this way you can have more than one event occurring in the select state change.

also advise you to modify the cidadesEstados.js to use a eventListener whenever possible:

this.estado.addEventListener("change", function(event) { 
    this.dgCidadesEstados.run()
};
  • Thanks @Toby, it worked

1

Try adding change with jquery:

$(document).ready(function(){ $("#estado").change(function(){ alert(123); }); });

1

The event .onchange is working properly.

It is being assigned on line 91 of the file cidadesEstados.js

this.estado.onchange= function() { this.dgCidadesEstados.run() };

In HTML you try to assign the function myfunction()" in the onchange event, you can only add one function per event (but you can call multiple functions within one). If you want to leave your function on onchange, I recommend refactor the code and create a select because there is no onchange event in text field.

Browser other questions tagged

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