Home City-state ruby gem javascript not working
Reply: 0

City-state ruby gem javascript not working

user1616
1#
user1616 Published in August 15, 2018, 5:37 am

I am using city-state gem(https://github.com/loureirorg/city-state) and trying to make a form which selects a state and then the city dynamically updatable with javascript depending on state which is selected but its not working.

My html form is:

<select id="states-of-country" name="project[state]">
<option id="no-state" value="" selected>Select Your State</option>  
<% CS.states(:us).each do |key, value| %>
<option value="<%= value %>" id="<%= key %>"><%= value %></option>
<% end %>
</select>      
<select id="cities-of-state" name="project[city]" class="form-control form-control-small" required>
<option value="" selected>Select Your City</option>
</select>

The javascript is:

   $(document).on('change', '#states-of-country', function(e) {
  var cities_of_state, input_state, state;
  input_state = $(this);
  cities_of_state = $('#cities-of-state');
  state = this.options[e.target.selectedIndex].id;
  if (state === 'no-state') {
    return cities_of_state.html('');
  } else {
    $.ajax({
      url: '/cities/' + $(this).children(':selected').attr('id'),
      success: function(data) {
        var opt;
        opt = '<option value="" selected>Select Your City</option>';
        if (data.length === 0) {} else {
          data.forEach(function(i) {
            opt += '<option value="' + i + '">' + i + '</option>';
          });
          cities_of_state.html(opt);
        }
      }
    });
  }
});
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.301673 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO