olimart
olimart

Reputation: 1569

more less example with coffeescript

I'm trying to add basic more/less links to toggle element visibility.

How do I insert preventDefault ?

Also it only works on first toggle, after that it doesn't toggle out. Any error?

$("#description-link").click ->
  $("#project-description").show()
  $(this).text("Show description")
, ->
  $("#project-description").hide()
  $(this).text("Hide description")

Upvotes: 1

Views: 202

Answers (2)

niiru
niiru

Reputation: 5112

To access preventDefault(), you need to add the event argument to your function, and call the preventDefault method on that:

$("#description-link").click (event) ->
  event.preventDefault()
  ....

Upvotes: 1

XNargaHuntress
XNargaHuntress

Reputation: 751

Well, you could try something like this instead:

$("#description-link").click (e) ->
  e.preventDefault()
  $('#project-description').toggle()
  a = $(this).text()
  a = if (a == 'Show Details') then 'Hide Details' else 'Show Details'
  $(this).text(a)

And apparently the space between click and (e) is required. Otherwise a TypeError is thrown.

EDIT: Just realized why the space is required...as it's part of the function declaration. Haven't really messed with Coffeescript before. /facepalm

Upvotes: 2

Related Questions