0

Below you can see two functions. The second works perfectly. It submits data when a select menu has been changed.

I am trying to apply a similar logic but this time to a field. When the value in the field as changed I want to the data to submit, as seen in the first bit of js

Despite the logic being almost the same I get this error

Uncaught ReferenceError: $ is not defined
9452:382 (anonymous function)

If someone could illuminate to me what exactly this and how I fix it I'd be very grateful. My assumption is that its something related to the order in which things load, however as the logic is no different from the old logic surely this shouldnt be a problem.

 :javascript

    $('#booking_package_#{package.id}').on("keyup change", function(){
      var bookingPackageQuantity = document.getElementById("menu_item_#{package.id}");
      $.ajax({
        url: "/venues/#{venue.id}/bookings/#{booking.id}/booking_packages/#{@booking_package.id}/add_menu_item?menu_item_id="+bookingPackageQuantity,
        type: "GET"
      })
    });


    $('#menu_item_#{package.id}').on("change", function(){
      var selects = document.getElementById("menu_item_#{package.id}");
      var menuItemId = selects.options[selects.selectedIndex].value
      $.ajax({
        url: "/venues/#{venue.id}/bookings/#{booking.id}/booking_packages/#{@booking_package.id}/add_menu_item?menu_item_id="+menuItemId,
        type: "GET"
      })
    });

I am trying to bind the event to this field

    = number_field_tag 'quantity', booking.find_package(package).quantity, id: "booking_package_#{package.id}"
4
  • Probably means that that code is imported before jQuery, or that something has called jQuery.noConflict();. Commented Feb 6, 2015 at 15:12
  • yah but why then is my other example working?! Commented Feb 6, 2015 at 15:13
  • did you try this? stackoverflow.com/questions/17158054/… Commented Feb 6, 2015 at 15:16
  • yes i did. I've tried it all Commented Feb 6, 2015 at 15:35

1 Answer 1

1

In the first part of the JS, please put value. Probably that may be breaking the javascript, please try the following edited code...

 $('#booking_package_#{package.id}').on("keyup change", function(){
  var bookingPackageQuantity = document.getElementById("menu_item_#{package.id}");
  $.ajax({
    url: "/venues/#{venue.id}/bookings/#{booking.id}/booking_packages/#{@booking_package.id}/add_menu_item?menu_item_id="+bookingPackageQuantity.value,
    type: "GET"
  })
});

Can you please let me know if you are using any frameworks for this? so that i can help you better...

Sign up to request clarification or add additional context in comments.

5 Comments

the above code didnt make a difference. I am using Ruby on Rails. I have jquery used throughout the application, all working. It just seems to be this that is causing the issue.
is it possible you can post a JSFiddle for this?
Once i enable the jquery in JSFiddle, i can see the alert message, which means to me its working? If so, please double check that the jquery is included properly
but the second example runs perfectly - this requires jquery.
Can you please place the full code in which you are facing issue in JSFiddle?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.