Dear all,

I'm new to Rails and Web development, I have a problem about how to
"bind a JS event to a field within form_for block".

I have a JS function called checkLength, which is used to check the
length of words typed in a text area field:


HTML code with JS event invocation
========================================

<textarea id="micropost" name="micropost" rows="4" cols="60"
onpropertychange="checkLength(this,5);" oninput="checkLength(this,
5);">


Now I would like to reuse this JS funtion in another Rails view page,
which the src code is listed below:


_micropost_form.html.erb
=========================================

<%= form_for @micropost do |f| %>
....
f.text_area :content
....
<% end %>

The question is after I put the JS function under RAILS_APP/public/
javascript as custom.js and include it with javascript_include_tag.

HOW CAN I ADD AN EVENT AT THE TEXT_AREA FIELD WITHIN THE CODE :
f_text_area :content AND CALL THE JS FUNCTION LIKE :
oninput="checkLength(this, 5)"

--
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.

Search Discussions

  • Pedro Fernandes Steimbruch at Oct 22, 2011 at 12:34 pm
    Have you ever heard about unobstrusive javascript?

    You write your html without any javascript, then you add the js behavior
    that you want in a separeted file.

    A simple search by "unobstrusive+javascript+<your js library>" will return a
    lot of material to read about.

    --
    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.
  • Radhames brito at Oct 23, 2011 at 11:58 am
    I apologize on behalf of pedro , the rails community is usually very kind to
    new comers.

    Rails usually comes with a javascript library, the default is jquery, if you
    generate a scaffold you will see it gets imported in the head tag

    <%= javascript_include_tag :defaults%>

    will import it.

    with it you can capture html element via a css selector and attach event to
    the element.

    http://railscasts.com/episodes/136-jquery

    that is an old tutorial but it will get you started.

    --
    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.
  • Pedro Fernandes Steimbruch at Oct 23, 2011 at 4:54 pm
    This is a very basic question, a simple google search and just a little
    reading about like I said will light your way.

    --
    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.
  • Radhames brito at Oct 23, 2011 at 7:12 pm

    On Sun, Oct 23, 2011 at 12:53 PM, Pedro Fernandes Steimbruch wrote:

    This is a very basic question, a simple google search and just a little
    reading about like I said will light your way.
    The web programming paradigm is very confusing for a beginner and the way
    rails makes things seem magical is even more confusing to some people, i
    often find easier to teach rails and web developing to someone completely
    new to programming.

    The form helper could be very obscure is you dont really know what helpers
    are. Also some basic javascript tutorials still have embedded js and to
    some the benefits of been unobtrusive are not obvious.

    Pauls seem to be confused not by javascript but by the form helper and the
    way it renders html at the end so he does not appears to know that the
    helper outputs an id form him to capture via js.

    --
    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.
  • Paul at Oct 24, 2011 at 1:56 am
    Hi Radhames,

    Thank you so much for your kindly help and patient. I found the answer
    by the link you point above.

    As you said, the question it's not about the JS itself but rather than
    how to render JS in Rails ERB file. After read though the text_area
    API, I got the answer.

    Next time I will start from reading API instead of posting such a
    native question. Thank you again

    Hi Pedro,

    Thank you for your suggestion and I found it help me a lot!
    On 10月24日, 上午3时11分, radhames brito wrote:
    On Sun, Oct 23, 2011 at 12:53 PM, Pedro Fernandes Steimbruch <

    pedrofsteimbr...@gmail.com> wrote:
    This is a very basic question, a simple google search and just a little
    reading about like I said will light your way.
    The web programming paradigm is very confusing for a beginner and the way
    rails makes things seem magical is even more confusing to some people, i
    often find easier to teach rails and web developing to someone completely
    new to programming.

    The form helper could be very obscure is you dont really know what helpers
    are. Also some basic javascript tutorials still have embedded js and to
    some the benefits of been unobtrusive are not obvious.

    Pauls seem to be confused not by javascript but by the form helper and the
    way it renders html at the end so he does not appears to know that the
    helper outputs an id form him to capture via js.
    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedOct 21, '11 at 4:18p
activeOct 24, '11 at 1:56a
posts6
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase