0

This does not seem to load on my sidebar. I think it has something to do with Turbolinks in rails 4 since if I reload the page, it seems to work. Any idea on how to change this to get it working?

jQuery ->
  category = $('#subject_category').html()
  $('#subject_product').change ->
    product = $('#subject_product :selected').text()
    escaped_product = product.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
    options = $(category).filter("optgroup[label='#{escaped_product}']").html()
    if options
      $('#subject_category').html(options)
      $('#subject_category').parent().show()
    else
      $('#subject_category').empty()
      $('#subject_category').parent().hide()

$ ->
  category = $('#sidebar .category_select').html()   
  options = $(category).filter("optgroup[label='Car']").html()
  if options
    $('#sidebar .category_select').html(options)
    $('#sidebar .category_select').parent().show()
  else
    $('#sidebar .category_select').empty()
    $('#sidebar .category_select').parent().hide()
user2012677
  • 5,465
  • 6
  • 51
  • 113
  • My issue is with the coffee script syntax and not sure the other post addresses my request. As these are not onload events – user2012677 Oct 16 '16 at 18:28
  • `$ -> ` is the short cut for `jQuery -> ` and that is the short cut for `document ready ` . This post describes whats different for `document ready` with turbolinks and I'm sure that's your issue. – slowjack2k Oct 16 '16 at 18:32
  • So I should replace `$ ->` with `$(document).on "turbolinks:load", ->`, is this correct? and what is `jQuery ->` a short cut for? Should I also replace `jQuery ->` with `$(document).on "turbolinks:load", ->`? – user2012677 Oct 16 '16 at 18:35

1 Answers1

0
$(document).on "turbolinks:load", ->
alert "page has loaded!"

There is also a gem - jquery.turbolinks gem. Just in case. Works on 4.2 and above. Below I wouldn't know.

Georg Keferböck
  • 1,967
  • 26
  • 43