0

I have one function that should be used in two different events, but I can't make it work. Where am I making a mistake? Or do I have to use Class instead?

coment_error = (that) ->
  $this = $(that)
  $new_answer = $this.parent('.new_answer')

  $new_answer.on('ajax:success',((evt, data, status, xhr)->
    $new_answer.hide()
    $('.open').show()
  ))

  $new_answer.on('ajax:error',((evt, data, status, xhr)->
    $(this).addClass("error")

    ))

$(document).on("click", ".new_answer > INPUT[type='submit']", coment_error($(this)))
$(document).on("click", ".new_comment > INPUT[type='submit']", coment_error($(this)))
2
  • Can you please post also your HTML because from that snippet of code alone it's hard to tell what's not working. Are you sure your jquery selectors are correct? Commented Oct 9, 2013 at 19:16
  • thx for correction..., davidfurber gave me right answer Commented Oct 9, 2013 at 20:11

1 Answer 1

2

I think the problem is that the last two lines execute the function immediately instead of when the events are called.

$(document).on("click", ".new_answer > INPUT[type='submit']", coment_error)

Also you don't want coment_error to receive "that" because "this" will automatically be bound to the clicked element. So you can get right down to $this = $(this).

Sign up to request clarification or add additional context in comments.

Comments

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.