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
?
The global Keyword Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.
In JavaScript, before using a variable, we need to declare and initialize it (assign value). Unlike JavaScript, while creating a variable in CoffeeScript, there is no need to declare it using the var keyword. We simply create a variable just by assigning a value to a literal as shown below.
An Immediate-Invoked Function Expression (IIFE) is a function that is executed instantly after it's defined. This pattern has been used to alias global variables, make variables and functions private and to ensure asynchronous code in loops are executed correctly.
To define a function here, we have to use a thin arrow (->). Behind the scenes, the CoffeeScript compiler converts the arrow in to the function definition in JavaScript as shown below. (function() {}); It is not mandatory to use the return keyword in CoffeeScript.
The problem is that you're probably referring to \u{441}heckbox_date
(different identifier) instead of checkbox_date
(as commented by @muistooshort though) at this line:
сheckbox_date.on 'click', ->
^
This с
should be the ASCII one. c
(\u{63}
or \u0063
).
Additionally, from the ECMAScript language (on which JavaScript`s are based) specifications they recommend Unicode Normalised Form C to be applied in the language source codes (for each implementation), what I believe to solve this problem (I never cared and never did nothing about this though), but optional anyways.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With