0

I am not familiar with coffee script, I try to move jquery from view to put in asset but not able to make it work.

Here the working from view:

- jquery_ready do
  $('label[for=voucher_name], input#voucher_name').hide();
  $( "#voucher_voucher_provider_id" ).change(function() {
  var exist_id = $(this).val();
  var ids = $('#voucher_false_ids_')[0].value;
  if(jQuery.inArray(exist_id, ids.split(" ")) !== -1){
  $('label[for=voucher_name], input#voucher_name').hide();
  }
  else
  {
  $('label[for=voucher_name], input#voucher_name').show();
  }
  });
                                                                                                              

Then in /app/assets/javascript/mycode.js.coffee

jQuery ->
  $('label[for=voucher_name], input#voucher_name').hide();
  $( "#voucher_voucher_provider_id" ).change ->
    exist_id = $(this).val();
    ids = $('#voucher_false_ids_')[0].value;
    alert('alert');
    If(jQuery.inArray(exist_id, ids.split(" ")) !== -1)
      $('label[for=voucher_name], input#voucher_name').hide();
    else
      $('label[for=voucher_name], input#voucher_name').show();

So far, I able to run until .change -> alert('alert'); Not after I start put all line after If

which cause error:

ExecJS::RuntimeError at /admin
SyntaxError: [stdin]:6:51: unexpected =

Help: for proper syntax or what is the error coming from /Thanks

3
  • 2
    Two things jump out: If should be if and !== should be !=. Or just leave the JavaScript as JavaScript. Commented Jul 22, 2021 at 17:12
  • It worked, would you please move you comment to answer for me to accept / Thanks Commented Jul 22, 2021 at 17:23
  • Feel free to go with Alex's answer, his answer has more details than my quick comment. Commented Jul 22, 2021 at 17:54

1 Answer 1

1

If you paste that snippet into the "Try CoffeScript" page of the CoffeeScript website, it tells you what's wrong.

It says this:

[stdin]:7:51: error: unexpected =
    If(jQuery.inArray(exist_id, ids.split(" ")) !== -1)
                                                  ^

CoffeeScript does not have === or !== operators. == and != are translated their strict equivalents when compiled for you.

If you fix that, you get another error:

[stdin]:8:1: error: unexpected indentation
      $('label[for=voucher_name], input#voucher_name').hide();
^^^^^^

This is because of the previous line. You start an if statement with If, so CoffeeScript does not see this as an if statement, but instead as a function call to the If() function.

Change that to lowercase if and then it compiles just fine.


Short version, here it is fixed:

jQuery ->
  $('label[for=voucher_name], input#voucher_name').hide();
  $( "#voucher_voucher_provider_id" ).change ->
    exist_id = $(this).val();
    ids = $('#voucher_false_ids_')[0].value;
    alert('alert');
    if(jQuery.inArray(exist_id, ids.split(" ")) != -1)
      $('label[for=voucher_name], input#voucher_name').hide();
    else
      $('label[for=voucher_name], input#voucher_name').show();
Sign up to request clarification or add additional context in comments.

1 Comment

If(jQuery.inArray(exist_id, ids.split(" ")) !== -1) is actually treated as a function call in both coffescript and JS. And yes JS will actually let you define a function named If.

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.