I have the following code.
(function() {
$(document).on("keydown", function(e) {
// ignore unless CTRL + ALT + N was pressed
if (!(e.keyCode == 78 && e.ctrlKey && e.altKey )) return
jQuery('[name*="type"]').select2("val","SECOND").trigger('change');
jQuery('[name*="size"]').val(5);
})
}())
Basically what it does is that when CTRL + ALT + N is pressed, it executes some jquery on the page. The issue that I am having is that when the "Type" field is changed, there is some JS that runs and makes the "size" field visible (It is only visible when type=SECOND
). So, in order for this to work at the moment, I have to press CTRL + ALT + N twice. The first time it sets the value of type, then a few seconds later once the page has changed, I do it again and it fills in the value of 5 for size.
Note that I have read Why does the jquery change event not trigger when I set the value of a select using val()? which helped me make the event trigger, but I am not quite there yet.
Main HTML
<%= simple_form_for @type, :url => type_path, :remote => true, :html => {:method => :post} do |f| %>
<%= form_errors @type, :header_text => 'There are problems with one or more fields:' %>
<%= hidden_field_tag "change_type" %>
<%= render :partial => 'type', :locals => {:f => f} %>
<%= render :partial => 'size1', :locals => {:f => f} %>
<div class="form__action">
<a class="button" href="<%= type_path %>">Cancel</a>
<%= f.submit "Save", :class => 'button button--primary' %>
</div>
<% end %>
size1 partial
<% if @type.second? %>
<%= render :partial => 'size2' %>
<% end %>
new.js.coffee
$ = jQuery
$('#type_show').on('change', "#thing_type", ->
$this = $(@)
original = $("#thing_type_original").val()
current = $this.val()
if current in ['FIRST', 'SECOND']
$("#allow_something")
.filter(":hidden")
.select2("val", "true")
$("#allow_something")
.closest(".form__row").fadeIn()
else
$("#allow_something")
.not(":disabled")
.select2("val", "false")
.closest(".form__row").fadeOut()
if (current is "SECOND" and original isnt "SECOND") or (current isnt "SECOND" and original is "SECOND")
$('#change_type').val('true')
$this.closest("form").submit()
)