Hi all,


Currently I'm working on a ruby on rails project and I have to say, I'm
loving all the neat conventions.

Now I would like to integrate some Javascript into my project, with the use
of Coffee script.

But I have to admit, I have little knowledge about Javascript, and even
less about Coffee script.

Having said that, I would still like to try to use it.


Anyway, what I want is this: I want a button on my webpage that, when
clicked, gives me an alert box.


What I would write in *HTML*:

<div id="test">

<button id="test_button">Test</button>

</div>


And in the included *JS* file:

window.onload = function() {

$("test_button").onclick = test_func;

}

function test_func(){

alert('it works!');

}


I tested this, and it does exactly what I want.


Now, the corresponding *HAML* code I'm using is:

.test

%button{:id => 'test_button'} Test


And in Coffee script:

test_func = -> alert 'it works!'

window.onload = -> $('test_button').onclick = test_func


This does create a button with the text 'Test' on there.

But when I try the code above, it does not preform any action..


I have the feeling it's because I'm unaware of some rails convention.

Can maybe somebody help me with my problem?


*Versions I'm using:*

- Ruby: 1.9.3p5
- Rails: 3.1.3
- Haml: 3.1.4
- Coffee-script: (2.2.0)

I hope this was enough information.

Please let me know if anything is unclear


Kind regards,


Ron

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/T0J5u9dHEUcJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Bryan Dunsmore at Dec 28, 2011 at 6:43 am
    It actually has to do with your jQuery code. `$()` actually returns a
    jQuery instance with all the jQuery methods. `onclick` is not an actual
    jQuery method; you need to use the alternative
    [`click`](http://api.jquery.com/click/) and pass it the function. Like
    so:

    window.onload = function() {
    $('#test_button').click(test_func);
    }

    function test_func() {
    alert('it works!');
    }

    P.S. `$()` is a CSS-selector. Ergo, passing it `test_button` means that
    you want the `test_button` tag. You should prefix it with the
    id-selector (#).

    --
    Posted via http://www.ruby-forum.com/.

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Ron Boortman at Dec 28, 2011 at 11:36 am
    Jeej, this finally works ^^.
    The CS code I've written is:
    test_func = -> alert 'it works!'
    window.onload = -> $('#test_button').click test_func

    Thanks a lot for your help

    --
    You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
    To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/GkYnraBt6SYJ.
    To post to this group, send email to rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedDec 27, '11 at 11:08p
activeDec 28, '11 at 11:36a
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Ron Boortman: 2 posts Bryan Dunsmore: 1 post

People

Translate

site design / logo © 2022 Grokbase