This is my first Coffeescript function and can't figure out how to get this to not refresh my page after the user clicks and the event is fired:
jQuery ->
$(".answer_link").click ->
$val = $(this).attr 'id'
$id = $val.replace(/answer_link_/, '')
$input = "#new_answer_" + $id
$($input).toggle 'slow'
Thanks!
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.
You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file. In other cases, I've seen people use the attributes of type="coffeescript" and type="coffee" , so they might work for you as well. Save this answer.
CoffeeScript is something that makes even good JavaScript code better. CoffeeScript compiled code can do everything that natively written JavaScript code can, only the code produced by using CoffeeScript is way shorter, and much easier to read.
I think this is a question about jQuery
or JavaScript
.
you can use .preventDefault()
to do this:
jQuery ->
$(".answer_link").click (event)->
#like this
event.preventDefault()
$val = $(this).attr 'id'
$id = $val.replace(/answer_link_/, '')
$input = "#new_answer_" + $id
$($input).toggle 'slow'
more info about preventDefault.
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