3

The event fires well on a wildcard selector, but fails if I use a specific ID for the selector.

This works:

$("*").click ->
  console.log "entered event."
  $("#tellafriend").dialog
    modal: true
    buttons:
      Ok: ->
        $(this).dialog "close"

Yet, this doesn't:

$("#tell-a-friend").click ->
  console.log "entered event."
  $("#tellafriend").dialog
    modal: true
    buttons:
      Ok: ->
        $(this).dialog "close"

And my relevant HTML:

<ul class="actions">
    <li><a href="#">Home</a></li>
    <li>|</li>
    <li><a href="#" id="tell-a-friend">Tell a Friend</a></li>
</ul>

Am I missing something in CoffeeScript for this to work? Maybe jQuery selectors are formatted differently in CoffeeScript?

This is the rendered Javascript that my application serves (Rails convert CoffeeScript to JS when serving the pages):

(function() {

  $("#tell-a-friend").click(function() {
    console.log("works");
    return $("#tellafriend").dialog({
      modal: true,
      buttons: {
        Ok: function() {
          return $(this).dialog("close");
        }
      }
    });
  });

}).call(this);
1
  • 3
    Oh you've got to be kidding me, I forgot the $(document).ready() bit. Testing. Edit: Yes, that was the problem. Massive brainfart on my part. This always happens when I'm in new territory (coffeescript) I forget the basics. Commented Feb 12, 2012 at 16:05

1 Answer 1

2

Is it possible that the code is being executed before the DOM is completely loaded. Try wrapping it in a document ready handler so that any click handlers are applied after the DOM is completely loaded. As it is, it may only be executing on the body element because that's the only element available at the time the script is executed.

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.