Saturday, January 28, 2017

Declare global variable in coffescript

Leave a Comment

I use Gem Coocon for nested forms.

I need to hide the #end_date_job_portfolio field if .checkBoxCurrentJob is checked. The problem is that jQuery can`t see the tags if the user didn`t click add new nested form because these tags aren`t in document yet.

For this I use the cocoon:after-insert. This my script:

$(document).ready ->    checkbox_date = ""   end_date = ""   $('.experiences').on 'cocoon:after-insert', ->     checkbox_date = $(".checkBoxCurrentJob")     end_date = $('#end_date_job_portfolio')      console.log(checkbox_date)      #show it when the checkbox is clicked   сheckbox_date.on 'click', ->     console.log("la la la")     if checkbox_date.prop('checked')       checkbox_date.hide()     else       checkbox_date.fadeIn()     return  

It displays ReferenceError: \u0441heckbox_date is not defined error. How do I pass checkbox_date and end_date values from cocoon:after-insert to the click event of checkbox_date?

2 Answers

Answers 1

You're doing it right, but it looks like you're refering to \u0441heckbox_date instead of checkbox_date (as commented by @muistooshort) at this line:

  сheckbox_date.on 'click', -> 

So this first "с" isn't "c" as you can see: 'с' === 'c'. The rest looks to be okay. So change it to:

  checkbox_date.on 'click', -> 

This is most likely a troll. Did anyone write a part of this for u?

Answers 2

jquery has support for things that come in and out:

$(document).ready ->        #show it when the checkbox is clicked   $(document).on 'click', ".checkBoxCurrentJob", ->     console.log("la la la")     checkbox_date = $(this)     if checkbox_date.prop('checked')       checkbox_date.hide()     else       checkbox_date.fadeIn()     return  
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment